extern "C" void * MYCDECL CUSTOM_REALLOC (void * ptr, size_t sz) { if (ptr == NULL) { ptr = internalMalloc (sz); return ptr; } if (sz == 0) { CUSTOM_FREE (ptr); return NULL; } size_t objSize = CUSTOM_GETSIZE (ptr); void * buf = internalMalloc(sz); if (buf != NULL) { if (objSize == CUSTOM_GETSIZE(buf)) { // The objects are the same actual size. // Free the new object and return the original. CUSTOM_FREE (buf); return ptr; } // Copy the contents of the original object // up to the size of the new block. size_t minSize = (objSize < sz) ? objSize : sz; memcpy (buf, ptr, minSize); } // Free the old block. CUSTOM_FREE (ptr); // Return a pointer to the new one. return buf; }
extern "C" void * MYCDECL CUSTOM_MEMALIGN (size_t alignment, size_t size) { // NOTE: This function is deprecated. if (alignment == sizeof(double)) { return internalMalloc (size); } else { void * ptr = internalMalloc (size + 2 * alignment); void * alignedPtr = (void *) (((size_t) ptr + alignment - 1) & ~(alignment - 1)); return alignedPtr; } }
extern "C" char * MYCDECL CUSTOM_STRDUP(const char * s) { char * newString = NULL; if (s != NULL) { if ((newString = (char *) internalMalloc(strlen(s) + 1))) { strcpy(newString, s); } } return newString; }
extern "C" void * MYCDECL CUSTOM_CALLOC(size_t nelem, size_t elsize) { size_t n = nelem * elsize; void * ptr = internalMalloc (n); // Zero out the malloc'd block. if (ptr != NULL) { memset (ptr, 0, n); } return ptr; }
void * operator new[] (size_t size) #if defined(__APPLE__) throw (std::bad_alloc) #endif { void * ptr = internalMalloc(size); if (ptr == NULL) { throw std::bad_alloc(); } else { return ptr; } }
extern "C" char * MYCDECL CUSTOM_STRNDUP(const char * s, size_t sz) { char * newString = NULL; if (s != NULL) { size_t cappedLength = strnlen (s, sz); if ((newString = (char *) internalMalloc(cappedLength + 1))) { strncpy(newString, s, cappedLength); newString[cappedLength] = '\0'; } } return newString; }
extern "C" char * MYCDECL CUSTOM_GETCWD(char * buf, size_t size) { static getcwdFunction * real_getcwd = reinterpret_cast<getcwdFunction *> (reinterpret_cast<intptr_t>(dlsym (RTLD_NEXT, "getcwd"))); if (!buf) { if (size == 0) { size = PATH_MAX; } buf = (char *) internalMalloc(size); } return (real_getcwd)(buf, size); }
void * operator new[] (size_t sz, const std::nothrow_t&) throw() { return internalMalloc (sz); }
extern "C" void * MYCDECL CUSTOM_MALLOC(size_t sz) { void * ptr = internalMalloc(sz); return ptr; }