F
Frederick Grim
okay so the code in question looks like:
namespace util {
template<typename C> inline void mmapw(C *& ptr, size_t length, int prot,
int flags, int fd, off_t offset) {
if((ptr = (C*)mmap(NULL,length,prot,flags,fd,offset)) == NULL) {
err_mesg("mmap failed");
exit(EXIT_FAILURE);
}
return;
}
// more wrappers
}
namespace Parser {
// lot of stuff
class XMLRobot : public ost::XMLStream {
private:
// Data types and private functions for parsing
// tons of decs
unsigned char *xml_string;
public:
// Constructors
XMLRobot(void);
// XMLRobot can either be instantiated with a file or a string of XML data
// However the constructors need to be very different as we need an explicit length
// For the time when a string is passed;
XMLRobot(const char *);
XMLRobot(unsigned char*, int);
XMLRobot(const XMLRobot&);
XMLRobot& operator=(const XMLRobot& b) {
this->name2movementmap = b.name2movementmap;
this->addlist = b.addlist;
this->is_file = b.is_file;
}
bool got_state_req(void) const { return get_state; }
void Close(void);
int Read(unsigned char*, int);
// tons more code
};
XMLRobot::XMLRobot(const char *filen):is_file(true),get_state(false),b_read(0){
int fd = util:
penw(filen, O_RDONLY);
struct stat fs;
util::fstatw(fd, &fs);
util::mmapw<unsigned char>(this->xml_string, fs.st_size, PROT_READ,MAP_SHARED, fd, 0);
this->t_bytes = fs.st_size;
this->xml_file = filen;
init_perf_hash();
util::closew(fd);
}
int XMLRobot::Read(unsigned char* buf, int len) {
if(this->b_read + len > this->t_bytes) {
int amt = this->t_bytes - this->b_read;
unsigned char *ptr = this->xml_string + this->b_read;
for(int i = 0; i < amt; i++, ptr++, buf++) { *buf = *ptr; }
this->b_read = this->t_bytes;
return amt;
} else {
std::cout << (const char*)this->xml_string << "\n";
unsigned char *ptr = this->xml_string;
ptr += this->b_read;
for(int i = 0; i < len; i++, ptr++, buf++) { *buf = *ptr; }
this->b_read += len;
return len;
}
}
so the seg fault is in the std::cout line directly above. I hope this makes the problem a bit more clear
as this is slowly driving me nuts. Thanks for the time
Fred
namespace util {
template<typename C> inline void mmapw(C *& ptr, size_t length, int prot,
int flags, int fd, off_t offset) {
if((ptr = (C*)mmap(NULL,length,prot,flags,fd,offset)) == NULL) {
err_mesg("mmap failed");
exit(EXIT_FAILURE);
}
return;
}
// more wrappers
}
namespace Parser {
// lot of stuff
class XMLRobot : public ost::XMLStream {
private:
// Data types and private functions for parsing
// tons of decs
unsigned char *xml_string;
public:
// Constructors
XMLRobot(void);
// XMLRobot can either be instantiated with a file or a string of XML data
// However the constructors need to be very different as we need an explicit length
// For the time when a string is passed;
XMLRobot(const char *);
XMLRobot(unsigned char*, int);
XMLRobot(const XMLRobot&);
XMLRobot& operator=(const XMLRobot& b) {
this->name2movementmap = b.name2movementmap;
this->addlist = b.addlist;
this->is_file = b.is_file;
}
bool got_state_req(void) const { return get_state; }
void Close(void);
int Read(unsigned char*, int);
// tons more code
};
XMLRobot::XMLRobot(const char *filen):is_file(true),get_state(false),b_read(0){
int fd = util:
struct stat fs;
util::fstatw(fd, &fs);
util::mmapw<unsigned char>(this->xml_string, fs.st_size, PROT_READ,MAP_SHARED, fd, 0);
this->t_bytes = fs.st_size;
this->xml_file = filen;
init_perf_hash();
util::closew(fd);
}
int XMLRobot::Read(unsigned char* buf, int len) {
if(this->b_read + len > this->t_bytes) {
int amt = this->t_bytes - this->b_read;
unsigned char *ptr = this->xml_string + this->b_read;
for(int i = 0; i < amt; i++, ptr++, buf++) { *buf = *ptr; }
this->b_read = this->t_bytes;
return amt;
} else {
std::cout << (const char*)this->xml_string << "\n";
unsigned char *ptr = this->xml_string;
ptr += this->b_read;
for(int i = 0; i < len; i++, ptr++, buf++) { *buf = *ptr; }
this->b_read += len;
return len;
}
}
so the seg fault is in the std::cout line directly above. I hope this makes the problem a bit more clear
as this is slowly driving me nuts. Thanks for the time
Fred