void *pov_malloc(size_t size, const char *file, int line, const char *msg) { void *block; size_t totalsize; #if defined(MEM_HEADER) MEMNODE *node; #endif #if defined(MEM_PREFILL) || defined(MEM_GUARD) char *memptr; size_t i; #endif #if defined(MEM_HEADER) if (size == 0) { Error("Attempt to malloc zero size block (File: %s Line: %d).", file, line); } #endif totalsize = size + NODESIZE + (MEM_GUARD_SIZE * 2); /* number of bytes allocated in OS */ block = (void *)MALLOC(totalsize); if (block == NULL) MAError(msg, size); #if defined(MEM_HEADER) node = (MEMNODE *) block; #endif #if defined(MEM_TAG) node->tag = MEMTAG_VALUE; #endif #if defined(MEM_TRACE) || defined(MEM_STATS) node->size = totalsize; #endif #if defined(MEM_TRACE) node->file = file; node->line = line; #endif #if defined(MEM_PREFILL) memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE; for(i = 0; i < size; i++) memptr[i] = mem_prefill_string[i % mem_prefill_string_len]; #endif #if defined(MEM_GUARD) memptr = (char *)block + NODESIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) memptr[i] = mem_guard_string[i % mem_guard_string_len]; memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) memptr[i] = mem_guard_string[i % mem_guard_string_len]; #endif #if defined(MEM_RECLAIM) add_node(node); #endif #if defined(MEM_STATS) mem_stats_alloc(totalsize, file, line); #endif return (void *)((char *)block + NODESIZE + MEM_GUARD_SIZE); }
void *pov_realloc(void *ptr, size_t size, const char *file, int line, const char *msg) { void *block; #if defined(MEM_STATS) size_t oldsize; #endif #if defined(MEM_HEADER) MEMNODE *node; #endif #if defined(MEM_RECLAIM) MEMNODE *prev; MEMNODE *next; #endif #if defined(MEM_PREFILL) || defined(MEM_GUARD) char *memptr; size_t i; #endif if (size == 0) { if (ptr) pov_free(ptr, file, line); return NULL; } else if (ptr == NULL) return pov_malloc(size, file, line, msg); block = (void *)((char *)ptr - NODESIZE - MEM_GUARD_SIZE); #if defined(MEM_GUARD) memptr = (char *)block + NODESIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) { if(memptr[i] != mem_guard_string[i % mem_guard_string_len]) { Warning(0, "Damaged start guard detected in resized block (File: %s Line: %d).", file, line); break; } } memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) { if(memptr[i] != mem_guard_string[i % mem_guard_string_len]) { Warning(0, "Damaged end guard detected in resized block (File: %s Line: %d).", file, line); break; } } #endif #if defined(MEM_HEADER) node = (MEMNODE *) block; #endif #if defined(MEM_TAG) if (node->tag != MEMTAG_VALUE) Error("Attempt to realloc invalid block (File: %s Line: %d).", file, line); node->tag = ~node->tag; #endif #if defined(MEM_RECLAIM) prev = node->prev; next = node->next; #endif #if defined(MEM_STATS) oldsize = ((MEMNODE *)block)->size; #if defined(MEM_PREFILL) memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE; for(i = size; i < oldsize - NODESIZE - (MEM_GUARD_SIZE * 2); i++) memptr[i] = mem_clear_string[i % mem_clear_string_len]; #endif #endif block = (void *)REALLOC(block, NODESIZE + (MEM_GUARD_SIZE * 2) + size); if (block == NULL) MAError(msg, size); #if defined(MEM_STATS) /* REALLOC does an implied FREE... */ mem_stats_free(oldsize); /* ...and an implied MALLOC... */ mem_stats_alloc(NODESIZE + (MEM_GUARD_SIZE * 2) + size, file, line); #if defined(MEM_PREFILL) memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE; for(i = oldsize - NODESIZE - (MEM_GUARD_SIZE * 2); i < size; i++) memptr[i] = mem_prefill_string[i % mem_prefill_string_len]; #endif #endif #if defined(MEM_HEADER) node = (MEMNODE *) block; #endif #if defined(MEM_TAG) node->tag = MEMTAG_VALUE; #endif #if defined(MEM_TRACE) || defined(MEM_STATS) node->size = size + NODESIZE + (MEM_GUARD_SIZE * 2); #endif #if defined(MEM_TRACE) node->file = file; node->line = line; #endif #if defined(MEM_GUARD) memptr = (char *)block + NODESIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) memptr[i] = mem_guard_string[i % mem_guard_string_len]; memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) memptr[i] = mem_guard_string[i % mem_guard_string_len]; #endif #if defined(MEM_RECLAIM) if (prev == NULL) memlist = node; else prev->next = node; if (node->next != NULL) node->next->prev = node; if (next != NULL) next->prev = node; #endif return (void *)((char *)block + NODESIZE + MEM_GUARD_SIZE); }
void *pov_malloc(size_t size, const char *file, int line, const char *msg) { void *block; size_t totalsize; #if defined(MEM_HEADER) MEMNODE *node; #endif #if defined(MEM_PREFILL) || defined(MEM_GUARD) char *memptr; size_t i; #endif #if defined(MEM_HEADER) if (size == 0) { // TODO MESSAGE Error("Attempt to malloc zero size block (File: %s Line: %d).", file, line); } #endif totalsize = size + NODESIZE + (MEM_GUARD_SIZE * 2); /* number of bytes allocated in OS */ block = reinterpret_cast<void *>(MALLOC(totalsize)); if (block == NULL) throw std::bad_alloc();; // TODO FIXME !!! // Parser::MAError(msg, (int)size); #if defined(MEM_HEADER) node = reinterpret_cast<MEMNODE *>(block); #endif #if defined(MEM_TAG) node->tag = MEMTAG_VALUE; #endif #if defined(MEM_TRACE) || defined(MEM_STATS) node->size = totalsize; #endif #if defined(MEM_TRACE) node->file = file; node->line = line; #endif #if defined(MEM_PREFILL) memptr = reinterpret_cast<char *>(block) + NODESIZE + MEM_GUARD_SIZE; for(i = 0; i < size; i++) memptr[i] = mem_prefill_string[i % mem_prefill_string_len]; #endif #if defined(MEM_GUARD) memptr = reinterpret_cast<char *>(block) + NODESIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) memptr[i] = mem_guard_string[i % mem_guard_string_len]; memptr = reinterpret_cast<char *>(block) + (reinterpret_cast<MEMNODE *>(block))->size - MEM_GUARD_SIZE; for(i = 0; i < MEM_GUARD_SIZE; i++) memptr[i] = mem_guard_string[i % mem_guard_string_len]; #endif #if defined(MEM_RECLAIM) add_node(node); #endif #if defined(MEM_STATS) mem_stats_alloc(totalsize, file, line); #endif return reinterpret_cast<void *>(reinterpret_cast<char *>(block) + NODESIZE + MEM_GUARD_SIZE); }