Mercurial > hg > forks > yadex
view src/memory.cc @ 107:20aa5a515896
Reformat one line /* */ comments to //
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 06 Oct 2014 12:42:55 +0300 |
parents | dad7faa96eea |
children |
line wrap: on
line source
/* * memory.cc * Memory allocation routines. * BW & RQ sometime in 1993 or 1994. */ /* This file is part of Yadex. Yadex incorporates code from DEU 5.21 that was put in the public domain in 1994 by Raphaël Quinet and Brendon Wyber. The rest of Yadex is Copyright © 1997-2003 André Majorel and others. 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ #include "yadex.h" /* Note from RQ: To prevent memory fragmentation on large blocks (greater than 1K), the size of all blocks is rounded up to 8K. Thus, "realloc" will move the block if and only if it has grown or shrunk enough to cross a 8K boundary. I don't do that for smaller blocks (smaller than 1K), because this would waste too much space if these blocks were rounded up to 8K. There are lots of "malloc"'s for very small strings (9 characters) or filenames, etc. Thanks to Craig Smith (bcs@cs.tamu.edu) for some of his ideas about memory fragmentation. */ #define SIZE_THRESHOLD 1024 #define SIZE_OF_BLOCK 4095 // actually, this is (size - 1) /* * Allocate memory with error checking */ void *GetMemory(unsigned long size) { void *ret; // Limit fragmentation on large blocks if (size >= SIZE_THRESHOLD) size = (size + SIZE_OF_BLOCK) & ~SIZE_OF_BLOCK; ret = malloc(size); if (ret == NULL) fatal_error("out of memory (cannot allocate %u bytes)", size); return ret; } /* * Reallocate memory with error checking */ void *ResizeMemory(void *old, unsigned long size) { void *ret; // Limit fragmentation on large blocks if (size >= SIZE_THRESHOLD) size = (size + SIZE_OF_BLOCK) & ~SIZE_OF_BLOCK; ret = realloc(old, size); if (ret == NULL) fatal_error("Out of memory (cannot reallocate %lu bytes)", size); return ret; } /* * Free memory */ void FreeMemory(void *ptr) { if (ptr != NULL) free(ptr); }