2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gb_alloc_temp.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 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,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GB_ALLOC_C
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
int MEMORY_count = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-24 03:14:12 +02:00
|
|
|
//#define DEBUG_ME
|
2014-10-18 15:23:29 +02:00
|
|
|
//#define DO_NOT_PRINT_MEMORY
|
2010-06-24 03:14:12 +02:00
|
|
|
|
2012-02-27 00:07:14 +01:00
|
|
|
#if defined(DEBUG_ME) || DEBUG_MEMORY
|
|
|
|
size_t MEMORY_size = 0;
|
2014-10-18 15:23:29 +02:00
|
|
|
size_t MEMORY_pool_size = 0;
|
2012-02-27 00:07:14 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-27 00:07:14 +01:00
|
|
|
#if DEBUG_MEMORY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-05 15:07:21 +01:00
|
|
|
static int _id = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
ALLOC *_alloc = NULL;
|
2013-03-30 00:33:01 +01:00
|
|
|
#ifdef PROJECT_EXEC
|
2009-02-12 17:43:25 +01:00
|
|
|
extern char *DEBUG_get_current_position(void);
|
2013-03-30 00:33:01 +01:00
|
|
|
#endif
|
2009-02-12 17:43:25 +01:00
|
|
|
FILE *MEMORY_log;
|
2008-03-26 17:28:18 +01:00
|
|
|
|
|
|
|
#elif OPTIMIZE_MEMORY
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
#define SIZE_INC 16
|
|
|
|
#define SIZE_SHIFT 4
|
|
|
|
#define REAL_SIZE(_size) (((_size) + (SIZE_INC - 1)) & ~(SIZE_INC - 1))
|
|
|
|
|
|
|
|
#define POOL_SIZE 16
|
2011-09-08 18:01:36 +02:00
|
|
|
#define POOL_MAX 128
|
2010-05-28 21:33:05 +02:00
|
|
|
|
|
|
|
#define POOL_MAX_LEN (POOL_SIZE * SIZE_INC)
|
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
static size_t *_pool[POOL_SIZE] = { 0 };
|
2010-05-28 21:33:05 +02:00
|
|
|
static int _pool_count[POOL_SIZE] = { 0 };
|
2014-10-18 15:23:29 +02:00
|
|
|
/*static size_t *_first_alloc = 0;
|
|
|
|
static size_t *_max_alloc = 0;*/
|
2010-05-28 21:33:05 +02:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
#endif
|
|
|
|
|
2008-03-26 17:28:18 +01:00
|
|
|
int THROW_MEMORY()
|
|
|
|
{
|
|
|
|
THROW(E_MEMORY);
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_init(void)
|
2008-03-23 02:02:27 +01:00
|
|
|
{
|
2009-02-12 17:43:25 +01:00
|
|
|
#if DEBUG_MEMORY
|
2009-06-22 13:16:34 +02:00
|
|
|
/*char path[256];
|
2009-02-12 17:43:25 +01:00
|
|
|
sprintf(path, "/tmp/gambas-memory-%d-%d.log", getuid(), getpid());
|
2009-06-22 13:16:34 +02:00
|
|
|
MEMORY_log = fopen(path, "w+");*/
|
|
|
|
MEMORY_log = stderr;
|
2009-02-12 17:43:25 +01:00
|
|
|
#endif
|
2008-03-23 02:02:27 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 00:07:14 +01:00
|
|
|
void MEMORY_clear_cache()
|
2008-03-23 02:02:27 +01:00
|
|
|
{
|
2010-05-28 21:33:05 +02:00
|
|
|
#if OPTIMIZE_MEMORY
|
|
|
|
int i;
|
|
|
|
void *ptr, *next;
|
2012-02-27 00:07:14 +01:00
|
|
|
size_t size = 0;
|
2010-05-28 21:33:05 +02:00
|
|
|
|
|
|
|
for (i = 0; i < POOL_SIZE; i++)
|
|
|
|
{
|
|
|
|
ptr = _pool[i];
|
|
|
|
while (ptr)
|
|
|
|
{
|
|
|
|
next = *((void **)ptr);
|
2012-02-27 00:07:14 +01:00
|
|
|
size += (i + 1) * SIZE_INC;
|
2014-10-18 15:23:29 +02:00
|
|
|
#ifdef DEBUG_ME
|
2012-02-27 00:07:14 +01:00
|
|
|
MEMORY_size -= (i + 1) * SIZE_INC;
|
|
|
|
#endif
|
2010-05-28 21:33:05 +02:00
|
|
|
free(ptr);
|
|
|
|
ptr = next;
|
|
|
|
}
|
2012-02-27 00:07:14 +01:00
|
|
|
_pool[i] = NULL;
|
|
|
|
_pool_count[i] = 0;
|
2010-05-28 21:33:05 +02:00
|
|
|
}
|
2012-02-27 00:07:14 +01:00
|
|
|
|
2014-10-18 15:23:29 +02:00
|
|
|
#ifdef DEBUG_ME
|
2012-02-27 00:07:14 +01:00
|
|
|
fprintf(stderr, "free %ld bytes [%ld / %p]\n", size, MEMORY_size, sbrk(0));
|
|
|
|
#endif
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
#endif
|
2012-02-27 00:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MEMORY_exit(void)
|
|
|
|
{
|
|
|
|
MEMORY_clear_cache();
|
2010-05-28 21:33:05 +02:00
|
|
|
|
2008-03-23 02:02:27 +01:00
|
|
|
#if DEBUG_MEMORY
|
2009-02-12 17:43:25 +01:00
|
|
|
if (MEMORY_count)
|
|
|
|
{
|
|
|
|
fprintf(MEMORY_log, "\n*************************************************\n");
|
2010-05-19 14:43:57 +02:00
|
|
|
fprintf(MEMORY_log, "warning: %d allocation(s) non freed.\n", MEMORY_count);
|
2009-02-12 17:43:25 +01:00
|
|
|
while (_alloc)
|
|
|
|
{
|
|
|
|
fprintf(MEMORY_log, "<%d>\n", _alloc->id);
|
|
|
|
_alloc = _alloc->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(MEMORY_log);
|
|
|
|
#else
|
|
|
|
if (MEMORY_count)
|
2010-12-21 15:05:42 +01:00
|
|
|
ERROR_warning("%d allocation(s) non freed.\n", MEMORY_count);
|
2008-03-23 02:02:27 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if OPTIMIZE_MEMORY
|
|
|
|
#else
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
#if DEBUG_MEMORY
|
|
|
|
char *MEMORY_where_am_i(const char *file, int line, const char *func)
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "[%s] %s:%d", file, func, line);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#if DEBUG_MEMORY
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_alloc(void *p_ptr, size_t size, const char *src)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ALLOC *alloc;
|
|
|
|
|
|
|
|
alloc = (ALLOC *)malloc(sizeof(ALLOC) + size);
|
|
|
|
if (!alloc)
|
|
|
|
THROW(E_MEMORY);
|
|
|
|
|
|
|
|
_id++;
|
|
|
|
alloc->id = _id;
|
|
|
|
alloc->prev = NULL;
|
|
|
|
alloc->next = _alloc;
|
|
|
|
alloc->size = size;
|
|
|
|
|
|
|
|
if (_alloc)
|
|
|
|
_alloc->prev = alloc;
|
|
|
|
|
|
|
|
_alloc = alloc;
|
|
|
|
|
|
|
|
*((void **)p_ptr) = (char *)alloc + sizeof(ALLOC);
|
|
|
|
MEMORY_count++;
|
|
|
|
MEMORY_size += size;
|
|
|
|
|
|
|
|
#ifndef DO_NOT_PRINT_MEMORY
|
2013-03-30 00:33:01 +01:00
|
|
|
#ifdef PROJECT_EXEC
|
2009-02-12 17:43:25 +01:00
|
|
|
fprintf(MEMORY_log, "%s: ", DEBUG_get_current_position());
|
2013-03-30 00:33:01 +01:00
|
|
|
#endif
|
2009-02-12 17:43:25 +01:00
|
|
|
fprintf(MEMORY_log, "<%d> %s: MEMORY_alloc(%d) -> %p\n", _id, src, (int)size, (char *)alloc + sizeof(ALLOC));
|
|
|
|
fflush(MEMORY_log);
|
2014-05-31 21:47:06 +02:00
|
|
|
/*if (_id == 2700)
|
|
|
|
BREAKPOINT();*/
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_alloc(void *p_ptr, size_t size)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
void *alloc;
|
|
|
|
|
|
|
|
alloc = malloc(size);
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
THROW(E_MEMORY);
|
|
|
|
|
|
|
|
*((void **)p_ptr) = alloc;
|
|
|
|
MEMORY_count++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG_MEMORY
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_alloc_zero(void *p_ptr, size_t size, const char *src)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
MEMORY_alloc(p_ptr, size, src);
|
|
|
|
memset(*((void **)p_ptr), 0, size);
|
|
|
|
}
|
|
|
|
#else
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_alloc_zero(void *p_ptr, size_t size)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
void *alloc;
|
|
|
|
|
|
|
|
alloc = calloc(size, 1);
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
THROW(E_MEMORY);
|
|
|
|
|
|
|
|
*((void **)p_ptr) = alloc;
|
|
|
|
MEMORY_count++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG_MEMORY
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_realloc(void *p_ptr, size_t size, const char *src)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ALLOC *alloc = (ALLOC *)(*((char **)p_ptr) - sizeof(ALLOC));
|
|
|
|
ALLOC *old = alloc;
|
|
|
|
|
2011-07-16 17:41:51 +02:00
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
MEMORY_free(p_ptr, src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
alloc = realloc(alloc, sizeof(ALLOC) + size);
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
THROW(E_MEMORY);
|
|
|
|
|
|
|
|
MEMORY_size += size - alloc->size;
|
|
|
|
|
|
|
|
if (_alloc == old)
|
|
|
|
_alloc = alloc;
|
|
|
|
|
|
|
|
if (alloc->prev)
|
|
|
|
alloc->prev->next = alloc;
|
|
|
|
if (alloc->next)
|
|
|
|
alloc->next->prev = alloc;
|
|
|
|
|
|
|
|
#ifndef DO_NOT_PRINT_MEMORY
|
2013-03-30 00:33:01 +01:00
|
|
|
#ifdef PROJECT_EXEC
|
2009-02-12 17:43:25 +01:00
|
|
|
fprintf(MEMORY_log, "%s: ", DEBUG_get_current_position());
|
2013-03-30 00:33:01 +01:00
|
|
|
#endif
|
2009-02-12 17:43:25 +01:00
|
|
|
fprintf(MEMORY_log, "<%d> %s: MEMORY_realloc(%p, %d) -> %p\n", alloc->id, src, *((void **)p_ptr), (int)size, (char *)alloc + sizeof(ALLOC));
|
|
|
|
fflush(MEMORY_log);
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
*((void **)p_ptr) = (char *)alloc + sizeof(ALLOC);
|
|
|
|
}
|
|
|
|
#else
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_realloc(void *p_ptr, size_t size)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
void *alloc = *((void **)p_ptr);
|
|
|
|
|
2011-07-16 17:41:51 +02:00
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
MEMORY_free(p_ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
alloc = realloc(alloc, size);
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
THROW(E_MEMORY);
|
|
|
|
|
|
|
|
*((void **)p_ptr) = alloc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG_MEMORY
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_free(void *p_ptr, const char *src)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ALLOC *alloc = (ALLOC *)(*((char **)p_ptr) - sizeof(ALLOC));
|
|
|
|
|
2008-03-23 02:02:27 +01:00
|
|
|
if (!(*((void **)p_ptr)))
|
|
|
|
return;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (alloc->prev)
|
|
|
|
alloc->prev->next = alloc->next;
|
|
|
|
if (alloc->next)
|
|
|
|
alloc->next->prev = alloc->prev;
|
|
|
|
|
|
|
|
if (alloc == _alloc)
|
|
|
|
_alloc = alloc->next;
|
|
|
|
|
|
|
|
#ifndef DO_NOT_PRINT_MEMORY
|
2013-03-30 00:33:01 +01:00
|
|
|
#ifdef PROJECT_EXEC
|
2009-02-12 17:43:25 +01:00
|
|
|
fprintf(MEMORY_log, "%s: ", DEBUG_get_current_position());
|
2013-03-30 00:33:01 +01:00
|
|
|
#endif
|
2009-02-12 17:43:25 +01:00
|
|
|
fprintf(MEMORY_log, "<%d> %s: MEMORY_free(%p)\n", alloc->id, src, (char *)alloc + sizeof(ALLOC));
|
|
|
|
fflush(MEMORY_log);
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MEMORY_size -= alloc->size;
|
|
|
|
|
|
|
|
//*((long *)alloc) = 0x31415926;
|
|
|
|
free(alloc);
|
|
|
|
|
|
|
|
*((void **)p_ptr) = NULL;
|
|
|
|
MEMORY_count--;
|
|
|
|
}
|
|
|
|
#else
|
2009-07-08 21:57:50 +02:00
|
|
|
void MEMORY_free(void *p_ptr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
void *alloc = *((void **)p_ptr);
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
if (!alloc)
|
|
|
|
return;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
free(alloc);
|
|
|
|
|
|
|
|
*((void **)p_ptr) = NULL;
|
|
|
|
MEMORY_count--;
|
|
|
|
}
|
|
|
|
#endif
|
2008-03-11 14:29:47 +01:00
|
|
|
|
|
|
|
#endif // OPTIMIZE_MEMORY
|
2010-05-28 21:33:05 +02:00
|
|
|
|
|
|
|
#if OPTIMIZE_MEMORY
|
|
|
|
|
2014-10-18 15:23:29 +02:00
|
|
|
//void DEBUG_print_current_backtrace(void);
|
|
|
|
//static bool _print_backtrace = FALSE;
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
void *my_malloc(size_t len)
|
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
size_t *ptr;
|
|
|
|
int size = REAL_SIZE((int)len + sizeof(size_t));
|
2010-05-28 21:33:05 +02:00
|
|
|
int pool = (size / SIZE_INC) - 1;
|
|
|
|
|
|
|
|
MEMORY_count++;
|
|
|
|
|
|
|
|
if (pool < POOL_SIZE)
|
|
|
|
{
|
|
|
|
if (_pool_count[pool])
|
|
|
|
{
|
|
|
|
ptr = _pool[pool];
|
2014-10-18 15:23:29 +02:00
|
|
|
#if defined(DEBUG_ME) && !defined(DO_NOT_PRINT_MEMORY)
|
|
|
|
MEMORY_pool_size -= size;
|
|
|
|
fprintf(stderr, "my_malloc: %d bytes from pool #%d -> %p (%p) [%ld / %ld]\n", size, pool, ptr + 1, sbrk(0), MEMORY_size - MEMORY_pool_size, MEMORY_size);
|
2010-05-28 21:33:05 +02:00
|
|
|
#endif
|
|
|
|
_pool[pool] = *((void **)ptr);
|
|
|
|
_pool_count[pool]--;
|
|
|
|
*ptr++ = size;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-27 00:07:14 +01:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
MEMORY_size += size;
|
|
|
|
#endif
|
2010-05-28 21:33:05 +02:00
|
|
|
ptr = malloc(size);
|
|
|
|
if (!ptr)
|
|
|
|
THROW_MEMORY();
|
2014-10-18 15:23:29 +02:00
|
|
|
#if defined(DEBUG_ME) && !defined(DO_NOT_PRINT_MEMORY)
|
|
|
|
fprintf(stderr, "my_malloc: %d bytes from malloc -> %p (%p) [%ld / %ld]\n", size, ptr + 1, sbrk(0), MEMORY_size - MEMORY_pool_size, MEMORY_size);
|
2010-05-28 21:33:05 +02:00
|
|
|
#endif
|
2014-10-18 15:23:29 +02:00
|
|
|
|
|
|
|
/*if (!_first_alloc)
|
|
|
|
_first_alloc = ptr;
|
|
|
|
if (ptr > _max_alloc && !_print_backtrace)
|
|
|
|
{
|
|
|
|
_max_alloc = ptr;
|
|
|
|
fprintf(stderr, "%ld\n", (char *)_max_alloc - (char *)_first_alloc + size);
|
|
|
|
_print_backtrace = TRUE;
|
|
|
|
DEBUG_print_current_backtrace();
|
|
|
|
_print_backtrace = FALSE;
|
|
|
|
}*/
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
*ptr++ = size;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void my_free(void *alloc)
|
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
size_t *ptr;
|
2010-05-28 21:33:05 +02:00
|
|
|
int size;
|
|
|
|
int pool;
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MEMORY_count--;
|
|
|
|
|
|
|
|
ptr = alloc;
|
|
|
|
ptr--;
|
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
size = (int)*ptr;
|
2010-05-28 21:33:05 +02:00
|
|
|
pool = (size / SIZE_INC) - 1;
|
|
|
|
|
2014-10-18 15:23:29 +02:00
|
|
|
//if (ptr < (_first_alloc + (_max_alloc - _first_alloc) / 2))
|
|
|
|
if (1)
|
2010-05-28 21:33:05 +02:00
|
|
|
{
|
2014-10-18 15:23:29 +02:00
|
|
|
if (pool < POOL_SIZE)
|
2010-05-28 21:33:05 +02:00
|
|
|
{
|
2014-10-18 15:23:29 +02:00
|
|
|
if (_pool_count[pool] < POOL_MAX)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ME) && !defined(DO_NOT_PRINT_MEMORY)
|
|
|
|
MEMORY_pool_size += size;
|
|
|
|
fprintf(stderr, "my_free: %p (%p) -> %d bytes to pool #%d [%ld / %ld]\n", alloc, sbrk(0), size, pool, MEMORY_size - MEMORY_pool_size, MEMORY_size);
|
|
|
|
#endif
|
|
|
|
*((void **)ptr) = _pool[pool];
|
|
|
|
_pool[pool] = ptr;
|
|
|
|
_pool_count[pool]++;
|
|
|
|
return;
|
|
|
|
}
|
2010-05-28 21:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-18 15:23:29 +02:00
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
#ifdef DEBUG_ME
|
2012-02-27 00:07:14 +01:00
|
|
|
MEMORY_size -= size;
|
2014-10-18 15:23:29 +02:00
|
|
|
#ifndef DO_NOT_PRINT_MEMORY
|
|
|
|
fprintf(stderr, "my_free: %p (%p) -> %d bytes freed [%ld / %ld]\n", alloc, sbrk(0), size, MEMORY_size - MEMORY_pool_size, MEMORY_size);
|
|
|
|
#endif
|
2010-05-28 21:33:05 +02:00
|
|
|
#endif
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *my_realloc(void *alloc, size_t new_len)
|
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
size_t *ptr;
|
2010-05-28 21:33:05 +02:00
|
|
|
int size;
|
|
|
|
int new_size;
|
|
|
|
|
2014-09-26 23:52:42 +02:00
|
|
|
if (!alloc)
|
|
|
|
return my_malloc(new_len);
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
ptr = alloc;
|
|
|
|
ptr--;
|
2010-05-29 02:11:55 +02:00
|
|
|
size = (int)*ptr;
|
2010-06-24 03:14:12 +02:00
|
|
|
new_size = REAL_SIZE(new_len + sizeof(size_t));
|
2010-05-28 21:33:05 +02:00
|
|
|
|
|
|
|
if (size == new_size)
|
|
|
|
return alloc;
|
|
|
|
|
2014-10-18 15:23:29 +02:00
|
|
|
#if defined(DEBUG_ME) && !defined(DO_NOT_PRINT_MEMORY)
|
|
|
|
fprintf(stderr, "my_realloc: %p (%p) -> %d ==> %d\n", alloc, sbrk(0), size, new_size);
|
2010-05-28 21:33:05 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (new_len == 0)
|
|
|
|
{
|
|
|
|
my_free(alloc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (size > POOL_MAX_LEN && new_size > POOL_MAX_LEN)
|
|
|
|
{
|
2012-02-27 00:07:14 +01:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
MEMORY_size += new_size - size;
|
2014-10-18 15:23:29 +02:00
|
|
|
#ifndef DO_NOT_PRINT_MEMORY
|
|
|
|
fprintf(stderr, "my_realloc: %p (%p) -> %d bytes reallocated to %d [%ld / %ld]\n", alloc, sbrk(0), size, new_size, MEMORY_size - MEMORY_pool_size, MEMORY_size);
|
|
|
|
#endif
|
2012-02-27 00:07:14 +01:00
|
|
|
#endif
|
2010-05-28 21:33:05 +02:00
|
|
|
ptr = realloc(ptr, new_size);
|
|
|
|
if (!ptr)
|
|
|
|
THROW_MEMORY();
|
|
|
|
*ptr++ = new_size;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
size_t *nptr = my_malloc(new_len);
|
2010-05-28 21:33:05 +02:00
|
|
|
|
|
|
|
nptr--;
|
|
|
|
|
|
|
|
if (new_size < size)
|
|
|
|
memcpy(nptr, ptr, new_size);
|
|
|
|
else
|
|
|
|
memcpy(nptr, ptr, size);
|
|
|
|
|
|
|
|
my_free(alloc);
|
|
|
|
|
|
|
|
*nptr++ = new_size;
|
|
|
|
return nptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|