/*
 * malloc.h
 *
 * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _MALLOC_H_
#define _MALLOC_H_

#define __CRT_HEAP_FLAG_READY    (1 << 0)
#define __CRT_HEAP_FLAG_ZEROFILL (1 << 1)
#define __CRT_HEAP_FLAG_BESTFIT  (1 << 2)

enum
{
    __CRT_HEAP_CORRUPTED,
    __CRT_HEAP_DOUBLE_FREE,
    __CRT_HEAP_BAD_POINTER,
    __CRT_HEAP_OUT_OF_MEMORY,
};

typedef struct
{
    uint32_t magic;
    void *base;
    size_t size;
    uint32_t flags;
    uintptr_t next_offset;
    void *mutex;
    void (*lock_mutex_proc)(void*);
    void (*unlock_mutex_proc)(void*);
    void (*problem)(int);
} __crt_heap_t;

void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);

#endif
