2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gb_alloc.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-21 01:04:10 +01:00
|
|
|
(c) 2000-2011 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __GB_ALLOC_H
|
|
|
|
#define __GB_ALLOC_H
|
|
|
|
|
|
|
|
#define DEBUG_MEMORY 0
|
2008-03-11 14:29:47 +01:00
|
|
|
#define OPTIMIZE_MEMORY 1
|
2007-12-30 17:41:49 +01:00
|
|
|
//#define DO_NOT_PRINT_MEMORY
|
|
|
|
|
|
|
|
#ifndef __GB_ALLOC_C
|
2008-01-05 15:07:21 +01:00
|
|
|
EXTERN int MEMORY_count;
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEBUG_MEMORY
|
|
|
|
|
2010-01-01 15:55:13 +01:00
|
|
|
#undef OPTIMIZE_MEMORY
|
|
|
|
#define OPTIMIZE_MEMORY 0
|
|
|
|
|
2009-06-16 02:17:25 +02:00
|
|
|
typedef
|
|
|
|
struct ALLOC {
|
|
|
|
int _void;
|
|
|
|
struct ALLOC *next;
|
|
|
|
struct ALLOC *prev;
|
|
|
|
int id;
|
|
|
|
size_t size;
|
|
|
|
}
|
|
|
|
PACKED
|
|
|
|
ALLOC;
|
|
|
|
|
2008-01-05 15:07:21 +01:00
|
|
|
EXTERN int MEMORY_size;
|
2009-02-12 17:43:25 +01:00
|
|
|
EXTERN FILE *MEMORY_log;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#define ALLOC(_ptr, _size, _src) MEMORY_alloc((void *)_ptr, _size, _src)
|
|
|
|
#define ALLOC_ZERO(_ptr, _size, _src) MEMORY_alloc_zero((void *)_ptr, _size, _src)
|
|
|
|
#define REALLOC(_ptr, _size, _src) MEMORY_realloc((void *)_ptr, _size, _src)
|
|
|
|
#define FREE(_ptr, _src) MEMORY_free((void *)_ptr, _src)
|
2008-03-19 15:32:30 +01:00
|
|
|
#define IFREE(_ptr, _src) FREE(&(_ptr), _src)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-06-16 02:17:25 +02:00
|
|
|
#define GET_ALLOC_ID(_ptr) (((ALLOC *)((char *)(_ptr) - sizeof(ALLOC)))->id)
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_alloc(void *p_ptr, size_t size, const char *src);
|
|
|
|
void MEMORY_alloc_zero(void *p_ptr, size_t size, const char *src);
|
|
|
|
void MEMORY_realloc(void *p_ptr, size_t size, const char *src);
|
|
|
|
void MEMORY_free(void *p_ptr, const char *src);
|
|
|
|
void MEMORY_check(void);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_verify(void);
|
|
|
|
void MEMORY_check_ptr(void *ptr);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
#elif OPTIMIZE_MEMORY
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
/*#define ALLOC(_ptr, _size, _src) (LIKELY((*(_ptr) = malloc(_size)) != 0) ? MEMORY_count++ : THROW_MEMORY())
|
2010-05-19 22:18:23 +02:00
|
|
|
#define ALLOC_ZERO(_ptr, _size, _src) (LIKELY((*(_ptr) = calloc(_size, 1)) != 0) ? MEMORY_count++ : THROW_MEMORY())
|
|
|
|
#define REALLOC(_ptr, _size, _src) (LIKELY((*(_ptr) = realloc(*(_ptr), _size)) != 0) ? 0 : THROW_MEMORY())
|
|
|
|
#define FREE(_ptr, _src) (LIKELY(*(_ptr) != 0) ? free(*(_ptr)), *(_ptr) = NULL, MEMORY_count-- : 0)
|
2010-05-28 21:33:05 +02:00
|
|
|
#define IFREE(_ptr, _src) (LIKELY(_ptr != 0) ? free(_ptr), MEMORY_count-- : 0)*/
|
|
|
|
|
|
|
|
#define ALLOC(_ptr, _size, _src) (*(_ptr) = my_malloc(_size))
|
|
|
|
#define ALLOC_ZERO(_ptr, _size, _src) (*(_ptr) = my_malloc(_size), memset(*(_ptr), 0, (_size)))
|
|
|
|
#define REALLOC(_ptr, _size, _src) (*(_ptr) = my_realloc(*(_ptr), (_size)))
|
|
|
|
#define FREE(_ptr, _src) (my_free(*(_ptr)), *(_ptr) = NULL)
|
|
|
|
#define IFREE(_ptr, _src) (my_free(_ptr))
|
|
|
|
|
|
|
|
void *my_malloc(size_t len);
|
|
|
|
void my_free(void *alloc);
|
|
|
|
void *my_realloc(void *alloc, size_t len);
|
2008-03-11 14:29:47 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define ALLOC(_ptr, _size, _src) MEMORY_alloc((void *)_ptr, _size)
|
|
|
|
#define ALLOC_ZERO(_ptr, _size, _src) MEMORY_alloc_zero((void *)_ptr, _size)
|
|
|
|
#define REALLOC(_ptr, _size, _src) MEMORY_realloc((void *)_ptr, _size)
|
|
|
|
#define FREE(_ptr, _src) MEMORY_free((void *)_ptr)
|
2008-03-19 15:32:30 +01:00
|
|
|
#define IFREE(_ptr, _src) FREE(&(_ptr), _src)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_alloc(void *p_ptr, size_t size);
|
|
|
|
void MEMORY_alloc_zero(void *p_ptr, size_t size);
|
|
|
|
void MEMORY_realloc(void *p_ptr, size_t size);
|
|
|
|
void MEMORY_free(void *p_ptr);
|
|
|
|
void MEMORY_check(void);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_init(void);
|
|
|
|
void MEMORY_exit(void);
|
2010-06-05 01:48:53 +02:00
|
|
|
int THROW_MEMORY();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|