1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/******************************************************************************
(C) Nick Marley, 2001 -
This software is distributed under the GNU Lesser General Public Licence.
Please read and understand the comments at the top of vf_iface.h before use!
FILE
$Workfile: vf_malloc.c $
$Revision: 1.2 $
$Author: tilda $
ORIGINAL AUTHOR
Nick Marley
DESCRIPTION
C standard library based memory allocation functions referenced by default in
the accompanying vf_malloc.c file. These are excluded from the build entirely
if VFORMAT_EXCLUDE_MALLOC is set in the build options.
MODIFICATION HISTORY
* $Log: vf_malloc_stdlib.c,v $
* Revision 1.2 2002/11/15 09:15:00 tilda
* IID638823 - Various portability issues.
*
* Revision 1.1 2002/10/26 15:57:11 tilda
* Initial Version
*
*******************************************************************************/
#if !defined(VFORMAT_EXCLUDE_MALLOC)
#ifndef NORCSID
static const char vf_malloc_stdlib_c_vss_id[] = "$Header: /cvsroot/vformat/src/vformat/src/vf_malloc_stdlib.c,v 1.2 2002/11/15 09:15:00 tilda Exp $";
#endif
/*=============================================================================*
ANSI C & System-wide Header Files
*============================================================================*/
#include <stdlib.h>
#include <stdio.h>
/*============================================================================*
Interface Header Files
*============================================================================*/
#include "vformat/vf_iface.h"
/*============================================================================*
Local Header File
*============================================================================*/
#include "vf_config.h"
#include "vf_malloc.h"
#include "vf_strings.h"
/*============================================================================*
Public Data
*============================================================================*/
/* None */
/*============================================================================*
Private Defines
*============================================================================*/
#define NUMMEMBLOCKS (1024)
#if defined(WIN) || defined(WIN32)
#define DEBUGBREAK _asm { int 3 }
#endif
#ifndef DEBUGBREAK
#define DEBUGBREAK
#endif
#define ALLOCBREAK (-1)
/*============================================================================*
Private Data Types
*============================================================================*/
typedef struct MEMBLOCK_T
{
void *p; /* The block */
uint32_t s; /* The size */
char *file; /* filename it was allocated in */
int line; /* line number */
}
MEMBLOCK_T;
/*============================================================================*
Private Function Prototypes
*============================================================================*/
#if defined(VFORMAT_MEM_DEBUG)
static void init_mem_debug(void);
static void atexit_dump_mem(void);
#endif
/*============================================================================*
Private Data
*============================================================================*/
static MEMBLOCK_T blocks[NUMMEMBLOCKS];
/*============================================================================*
Public Function Implementations
*============================================================================*/
/*----------------------------------------------------------------------------*
* NAME
* _vf_stdlib_malloc()
*
* DESCRIPTION
* vformat compatible memory allocator using C standard library functions.
*
* RETURNS
* Ptr to new block, or NULL if failed.
*----------------------------------------------------------------------------*/
void *_vf_stdlib_malloc(
uint32_t s /* Size required */
#if defined(VFORMAT_MEM_DEBUG)
, const char *file, /* filename */
int line /* line number */
#endif
)
{
void *p = NULL;
#if defined(VFORMAT_MEM_DEBUG)
uint32_t i;
init_mem_debug();
for (i = 0;i < NUMMEMBLOCKS;i++)
{
if (blocks[i].p)
{
}
else
{
p = malloc(s);
if (p)
{
blocks[i].p = p;
blocks[i].s = s;
blocks[i].file = (char *)file;
blocks[i].line = line;
}
if (i == ALLOCBREAK)
{
DEBUGBREAK;
}
break;
}
}
#else /*defined(VFORMAT_MEM_DEBUG)*/
p = malloc(s);
#endif
return p;
}
/*----------------------------------------------------------------------------*
* NAME
* _vf_stdlib_realloc()
*
* DESCRIPTION
* Re-allocate chunk of memory allocated by _vf_malloc().
*
* RETURNS
* Ptr to new block, or NULL if failed.
*----------------------------------------------------------------------------*/
void *_vf_stdlib_realloc(
void *p, /* Original pointer */
uint32_t s /* Size required */
#if defined(VFORMAT_MEM_DEBUG)
, const char *file, /* Filename */
int line /* Line number */
#endif
)
{
void *np = NULL;
#if defined(VFORMAT_MEM_DEBUG)
uint32_t i;
init_mem_debug();
if (!p)
return _vf_stdlib_malloc(s, file, line);
for (i = 0;i < NUMMEMBLOCKS;i++)
{
if (blocks[i].p == p)
{
np = realloc(p, s);
if (np)
{
blocks[i].p = np;
blocks[i].s = s;
}
break;
}
}
#else /*defined(VFORMAT_MEM_DEBUG)*/
np = realloc(p, s);
#endif /*defined(VFORMAT_MEM_DEBUG)*/
return np;
}
/*----------------------------------------------------------------------------*
* NAME
* _vf_stdlib_free()
*
* DESCRIPTION
* De-allocate chunk of memory allocated by _vf_malloc().
*
* RETURNS
* (none)
*----------------------------------------------------------------------------*/
void _vf_stdlib_free(
void *p /* Pointer */
#if defined(VFORMAT_MEM_DEBUG)
, const char *file, /* Filename */
int line /* Line number */
#endif
)
{
#if defined(VFORMAT_MEM_DEBUG)
uint32_t i;
init_mem_debug();
for (i = 0;i < NUMMEMBLOCKS;i++)
{
if (blocks[i].p == p)
{
blocks[i].p = NULL;
blocks[i].s = 0;
blocks[i].line = 0;
blocks[i].file = NULL;
free(p);
break;
}
}
#else
free(p);
#endif
}
/*----------------------------------------------------------------------------*
* NAME
* vf_stdlib_dump_alloc_info()
*
* DESCRIPTION
* Display current state of memory allocator.
*
* RETURNS
* TRUE if blocks are currently allocated, FALSE else.
*----------------------------------------------------------------------------*/
bool_t vf_stdlib_dump_alloc_info()
{
unsigned long i;
unsigned long c, s;
for (i = 0, c = 0, s = 0;i < NUMMEMBLOCKS;i++)
{
if (blocks[i].p)
{
c += 1;
s += blocks[i].s;
printf("block[%lu] size %lu, file %s, line %d\n",
i, (unsigned long)blocks[i].s, blocks[i].file, blocks[i].line);
}
}
if (c)
{
printf("... a total of %lu blocks, %lu bytes\n", c, s);
}
return (0 < c) ? TRUE : FALSE;
}
/*============================================================================*
Private Function Implementations
*============================================================================*/
#if defined(VFORMAT_MEM_DEBUG)
/*----------------------------------------------------------------------------*
* NAME
* init_mem_debug()
*
* DESCRIPTION
* Initialisation function.
*
* RETURNS
* (none)
*----------------------------------------------------------------------------*/
static void init_mem_debug()
{
static bool_t init_yet = FALSE;
if (init_yet)
{
/* OK */
}
else
{
p_memset(blocks, '\0', sizeof(blocks));
atexit(atexit_dump_mem);
init_yet = TRUE;
}
}
/*----------------------------------------------------------------------------*
* NAME
* atexit_dump_mem()
*
* DESCRIPTION
* atexit() callback function which dumps memory allocated info.
*
* RETURNS
* (none)
*----------------------------------------------------------------------------*/
static void atexit_dump_mem(void)
{
vf_stdlib_dump_alloc_info();
}
#endif /*defined(VFORMAT_MEM_DEBUG)*/
#endif /*defined(VFORMAT_EXCLUDE_MALLOC)*/
/*============================================================================*
End Of File
*============================================================================*/
|