/*
 *   Pure Data Packet system file: memory allocation
 *   Copyright (c) by Tom Schouten <tom@zwizwa.be>
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <pf/mem.h>
#include <pf/stream.h>


#ifndef PF_ALLOC_DEBUG

/* malloc wrapper  */
void *pf_alloc(int size)
{
    void *ptr = malloc(size);
    if (!ptr){
	// i don't see a gentle way to deal with these. they need to
	// be handled upstream. just print a warning message.
	pf_post("FATAL: out of memory.");
	if (PF_DEBUG){ 
	    PF_ASSERT(ptr);
	}
	else {
	    exit(1);
	}
    }
    int *i = (int *)ptr;
    return ptr;
}


void pf_dealloc(void *stuff)
{
    //pf_post("dealloc %p", stuff);
    PF_ASSERT(stuff);
    int *i = (int *)stuff;
    //pf_post("dealloc ptr = %p, size = %d, content = %p : %s", i, i[-1], i[0], i);
    free (stuff);
}

void *pf_realloc(void *stuff, int size)
{
    PF_ASSERT(stuff);

    void *ptr = realloc(stuff, size);
    return ptr;
}

#else

#define CHUNK_MAGIC 0xdeadf00d

static int nb_chunks = 0;
#define max_nb_chunks 100000
static void* chunks[max_nb_chunks];
static int *bad_chunk = (int*)0x81a6880; //0x81a6878;

#define NB_INTS(bytes) (((bytes-1)/sizeof(int)) + 1)

void pf_check_chunk(void *stuff){
    int *i = ((int *)stuff) - 2;
    PF_ASSERT(i[1] == CHUNK_MAGIC);
    int n = i[0];
    if (n != CHUNK_MAGIC) {
	PF_ASSERT(i[n+2] == CHUNK_MAGIC);
    }
}

void pf_check_all_chunks(void){
    int i;
    for (i=0; i<nb_chunks; i++) pf_check_chunk(chunks[i]);
}

void got_bad_chunk(void){
}

void *pf_alloc(int bytes){
    //pf_post("pf_alloc(%d)", bytes);
    int n = NB_INTS(bytes); // get nb of words
    int *i = malloc((n + 3)*sizeof(int));
    i[0] = n;
    i[1] = CHUNK_MAGIC;
    i[n+2] = CHUNK_MAGIC;
    i = i + 2;
    void *ptr = i;
    PF_ASSERT(nb_chunks < max_nb_chunks);
    chunks[nb_chunks++] = ptr;
    pf_check_chunk(ptr);
    if (((int*)ptr) == bad_chunk) got_bad_chunk();
    return ptr;
}


void pf_dealloc(void *stuff)
{
    if (((int*)stuff) == bad_chunk) got_bad_chunk();
    pf_check_chunk(stuff);
    int *i = ((int *)stuff) - 2;
    PF_ASSERT(i[0] != CHUNK_MAGIC);
    i[0] = CHUNK_MAGIC;
    //free (stuff - (2*sizeof(int)));
}

void *pf_realloc(void *stuff, int size)
{
    pf_check_chunk(stuff);
    int *i = ((int *)stuff) - 2;
    int n = i[0];

    void *new_stuff = pf_alloc(size);
    memcpy(new_stuff, stuff, n * sizeof(int));
    pf_dealloc(stuff);
    return new_stuff;
}


#endif

