/* * Pure Data Packet system file: memory allocation * Copyright (c) by Tom Schouten * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ #include #include #include #include #ifndef PF_ALLOC_DEBUG /* malloc wrapper */ void *pf_alloc(int size) { void *ptr = malloc(size); if (!ptr){ // i don't see a gentle way to deal with these. they need to // be handled upstream. just print a warning message. pf_post("FATAL: out of memory."); if (PF_DEBUG){ PF_ASSERT(ptr); } else { exit(1); } } int *i = (int *)ptr; return ptr; } void pf_dealloc(void *stuff) { //pf_post("dealloc %p", stuff); PF_ASSERT(stuff); int *i = (int *)stuff; //pf_post("dealloc ptr = %p, size = %d, content = %p : %s", i, i[-1], i[0], i); free (stuff); } void *pf_realloc(void *stuff, int size) { PF_ASSERT(stuff); void *ptr = realloc(stuff, size); return ptr; } #else #define CHUNK_MAGIC 0xdeadf00d static int nb_chunks = 0; #define max_nb_chunks 100000 static void* chunks[max_nb_chunks]; static int *bad_chunk = (int*)0x81a6880; //0x81a6878; #define NB_INTS(bytes) (((bytes-1)/sizeof(int)) + 1) void pf_check_chunk(void *stuff){ int *i = ((int *)stuff) - 2; PF_ASSERT(i[1] == CHUNK_MAGIC); int n = i[0]; if (n != CHUNK_MAGIC) { PF_ASSERT(i[n+2] == CHUNK_MAGIC); } } void pf_check_all_chunks(void){ int i; for (i=0; i