logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

mem_open, mem_check, mem_alloc, mem_realloc, mem_free, mem_close - memory allocation routines for

Author

       Written by Abraham vd Merwe <abz@blio.com>

Unix                                               August 2004                                  MEM_OPEN(3debug)

Description

mem_open() initializes the memory debugging system. It should be called before any of the other routines.
       You can specify a callback function which should be called whenever something bad  happens,  or  NULL  in
       which  case  the  default error handler will be used. The default error handler logs error messages using
       the debug logging routines and exit.

       mem_check() check all the allocated memory areas. This is called every time memory is allocated or freed.
       You can also call it anytime you think memory might be corrupted.

       mem_alloc() allocates size bytes and returns a pointer  to  the  allocated  memory.  The  memory  is  not
       cleared.

       mem_realloc()  changes the size of the memory block pointed to by ptr to size bytes. The contents will be
       unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized.  If  ptr
       is  NULL,  the call is equivalent to mem_alloc(size); if size is equal to zero, the call is equivalent to
       mem_free(ptr). Unless ptr is NULL, it must have been returned  by  an  earlier  call  to  mem_alloc()  or
       mem_realloc().

       mem_free()  frees the memory space pointed to by ptr, which must have been returned by a previous call to
       mem_alloc() or mem_realloc(). If ptr is NULL, no operation is performed.

       mem_close() checks for leaks and possible memory corruptions.

Libraries

       Debug Library (-ldebug)

Name

       mem_open,  mem_check,  mem_alloc,  mem_realloc,  mem_free,  mem_close  -  memory  allocation routines for
       debugging

Notes

       If  the default fail callback is used or if this routines are combined with the log routines, care should
       be taken to call open and close functions in the right order. The correct order is as follows:

              mem_open (NULL);
              log_open (NULL,LOG_NORMAL,LOG_HAVE_COLORS | LOG_PRINT_FUNCTION);
              atexit (mem_close);
              atexit (log_close);

       Of course, atexit(3) should only be used if the program will not forked.

       None of the libdebug routines are  thread-safe.  I'm  not  planning  to  change  this  either!  For  more
       information, please see http://threading.2038bug.com/

Return Value

       For mem_alloc(), the value returned is a pointer to the allocated memory, which is suitably  aligned  for
       any kind of variable, or NULL if the request fails.

       mem_realloc()  returns a pointer to the newly allocated memory, which is suitably aligned for any kind of
       variable and may be different from ptr, or NULL if the request fails or  if  size  was  equal  to  0.  If
       mem_realloc() fails the original block is left untouched - it is not freed or moved.

       All other functions returns no value.

See Also

log_open(3), atexit(3)

Synopsis

#include<debug/memory.h>voidmem_open(void(*fail)(constchar*fmt,...));voidmem_check(void);void*mem_alloc(size_tsize);void*mem_realloc(void*ptr,size_tsize);voidmem_free(void*ptr);voidmem_close(void);

See Also