void Mem::gbfree ( void *ptr , const char *note, size_t size , bool checksize ) { if(!s_lock.working) return; logTrace( g_conf.m_logTraceMem, "ptr=%p size=%zu note='%s'", ptr, size, note ); if ((checksize && size == 0) || !ptr) { return; } // . get how much it was from the mem table // . this is used for alloc/free wrappers for zlib because it does // not give us a size to free when it calls our mfree(), so we use -1 int32_t slot = g_mem.getMemSlot ( ptr ); if ( slot < 0 ) { log(LOG_LOGIC,"mem: could not find slot (note=%s)",note); // do NOT abort here... Let it run, otherwise it dies during merges. abort(); // return for now so procog does not core all the time! return; } bool isnew = s_isnew[slot]; // if this returns false it was an unbalanced free if (!rmMem(ptr, size, note, checksize)) { return; } if ( isnew ) sysfree ( (char *)ptr ); else sysfree ( (char *)ptr - UNDERPAD ); }
void Mem::gbfree ( void *ptr , int size , const char *note ) { logTrace( g_conf.m_logTraceMem, "ptr=%p size=%d note='%s'", ptr, size, note ); // don't let electric fence zap us //if ( size == 0 && ptr==(void *)0x7fffffff) return; if ( size == 0 ) return; // huh? if ( ! ptr ) return; // . get how much it was from the mem table // . this is used for alloc/free wrappers for zlib because it does // not give us a size to free when it calls our mfree(), so we use -1 int32_t slot = g_mem.getMemSlot ( ptr ); if ( slot < 0 ) { log(LOG_LOGIC,"mem: could not find slot (note=%s)",note); //Do NOT abort here... Let it run, otherwise it dies during merges. abort(); //log(LOG_LOGIC,"mem: FIXME!!!"); // return for now so procog does not core all the time! return; //char *xx = NULL; *xx = 0; } bool isnew = s_isnew[slot]; // if this returns false it was an unbalanced free if ( ! rmMem ( ptr , size , note ) ) return; g_inMemFunction = true; if ( isnew ) sysfree ( (char *)ptr ); else sysfree ( (char *)ptr - UNDERPAD ); g_inMemFunction = false; }
void *Mem::gbrealloc ( void *ptr , size_t oldSize , size_t newSize , const char *note ) { logTrace( g_conf.m_logTraceMem, "ptr=%p oldSize=%zu newSize=%zu note='%s'", ptr, oldSize, newSize, note ); // return dummy values since realloc() returns NULL if failed if ( oldSize == 0 && newSize == 0 ) return (void *)0x7fffffff; // do nothing if size is same if ( oldSize == newSize ) return ptr; // if newSize is 0... if ( newSize == 0 ) { gbfree(ptr, note, oldSize, true); return (void *)0x7fffffff; } retry: // hack so hostid #0 can use more mem size_t max = g_conf.m_maxMem; //if ( g_hostdb.m_hostId == 0 ) max += 2000000000; // don't go over max if ( g_mem.getUsedMem() + newSize - oldSize >= max ) { // try to free temp mem. returns true if it freed some. if ( freeCacheMem() ) goto retry; g_errno = ENOMEM; log( LOG_WARN, "mem: realloc(%zu,%zu): Out of memory.",oldSize,newSize); return NULL; } // if oldSize is 0, use our malloc() instead if ( oldSize == 0 ) { return gbmalloc ( newSize , note ); } // assume it will be successful. we can't call rmMem() after // calling sysrealloc() because it will mess up our MAGICCHAR buf rmMem(ptr, oldSize, note, true); // . do the actual realloc // . CAUTION: don't pass in 0x7fffffff in as "ptr" // . this was causing problems char *mem = (char *)sysrealloc ( (char *)ptr - UNDERPAD , newSize + UNDERPAD + OVERPAD ); // remove old guy on sucess if ( mem ) { addMem ( (char *)mem + UNDERPAD , newSize , note , 0 ); char *returnMem = mem + UNDERPAD; // set magic char bytes for mem for ( int32_t i = 0 ; i < UNDERPAD ; i++ ) returnMem[0-i-1] = MAGICCHAR; for ( int32_t i = 0 ; i < OVERPAD ; i++ ) returnMem[0+newSize+i] = MAGICCHAR; return returnMem; } // ok, just try using malloc then! mem = (char *)mmalloc ( newSize , note ); // bail on error if ( ! mem ) { g_mem.m_outOfMems++; // restore the original buf we tried to grow addMem ( ptr , oldSize , note , 0 ); errno = g_errno = ENOMEM; return NULL; } // log a note log(LOG_INFO,"mem: had to use malloc+memcpy instead of realloc."); // copy over to it memcpy ( mem, ptr, oldSize ); // we already called rmMem() so don't double call sysfree ( (char *)ptr - UNDERPAD ); return mem; }