/*
 * heap.h
 *
 * Copyright (C) 2013 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 _HEAP_H_
#define _HEAP_H_

#include <common.h>
#include <lock.h>
#include <crt/include/malloc.h>

#define SYSTEM_HEAP_START    0x90000000
#define SYSTEM_HEAP_END      (EVICTABLE_HEAP_START - 1)
#define EVICTABLE_HEAP_START 0xA0000000
#define EVICTABLE_HEAP_END   KERNEL_POOL_END

#define SYSTEM_HEAP_MAGIC    0x74737953 // Syst
#define EVICTABLE_HEAP_MAGIC 0x63697645 // Evic

#define HEAP_ZEROFILL  (1 << 0)
#define HEAP_EVICTABLE (1 << 1)

typedef struct _heap_header_t
{
    dword_t length;
    bool_t free;
    dword_t magic;
} heap_header_t;

typedef struct
{
    __crt_heap_t crt;
    lock_t lock;
} heap_t;

extern heap_t system_heap;
extern heap_t evictable_heap;

void *heap_alloc(heap_t *heap, uintptr_t size);
void heap_free(heap_t *heap, void *ptr);
void *heap_realloc(heap_t *heap, void *ptr, uintptr_t size);
dword_t heap_create(heap_t *heap, uintptr_t start, uintptr_t end, dword_t flags, dword_t magic);
dword_t heap_destroy(heap_t *heap);
void heap_init(void);

#endif
