2017-01-23 06:19:43 +00:00
|
|
|
/*
|
|
|
|
4coder_mem.h - Preversioning
|
|
|
|
no warranty implied; use at your own risk
|
|
|
|
|
|
|
|
This software is in the public domain. Where that dedication is not
|
|
|
|
recognized, you are granted a perpetual, irrevocable license to copy,
|
|
|
|
distribute, and modify this file as you see fit.
|
|
|
|
*/
|
2016-05-25 02:12:49 +00:00
|
|
|
|
2017-02-18 01:04:41 +00:00
|
|
|
// TOP
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
#if !defined(FCODER_MEM_H)
|
2016-05-25 02:12:49 +00:00
|
|
|
#define FCODER_MEM_H
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
// 4tech_standard_preamble.h
|
|
|
|
#if !defined(FTECH_INTEGERS)
|
|
|
|
#define FTECH_INTEGERS
|
|
|
|
#include <stdint.h>
|
|
|
|
typedef int8_t i8_4tech;
|
|
|
|
typedef int16_t i16_4tech;
|
|
|
|
typedef int32_t i32_4tech;
|
|
|
|
typedef int64_t i64_4tech;
|
|
|
|
|
|
|
|
typedef uint8_t u8_4tech;
|
|
|
|
typedef uint16_t u16_4tech;
|
|
|
|
typedef uint32_t u32_4tech;
|
|
|
|
typedef uint64_t u64_4tech;
|
|
|
|
|
2017-02-18 01:04:41 +00:00
|
|
|
typedef u64_4tech umem_4tech;
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
typedef float f32_4tech;
|
|
|
|
typedef double f64_4tech;
|
|
|
|
|
|
|
|
typedef int8_t b8_4tech;
|
|
|
|
typedef int32_t b32_4tech;
|
2016-08-31 01:05:24 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
#if !defined(Assert)
|
|
|
|
# define Assert(n) do{ if (!(n)) *(int*)0 = 0xA11E; }while(0)
|
|
|
|
#endif
|
|
|
|
// standard preamble end
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-05-25 02:12:49 +00:00
|
|
|
struct Partition{
|
|
|
|
char *base;
|
2017-03-23 19:16:39 +00:00
|
|
|
i32_4tech pos;
|
|
|
|
i32_4tech max;
|
2016-05-25 02:12:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Temp_Memory{
|
|
|
|
void *handle;
|
2017-03-23 19:16:39 +00:00
|
|
|
i32_4tech pos;
|
2016-05-25 02:12:49 +00:00
|
|
|
};
|
|
|
|
|
2016-12-08 17:16:50 +00:00
|
|
|
struct Tail_Temp_Partition{
|
|
|
|
Partition part;
|
|
|
|
void *handle;
|
2017-03-23 19:16:39 +00:00
|
|
|
i32_4tech old_max;
|
2016-12-08 17:16:50 +00:00
|
|
|
};
|
|
|
|
|
2016-05-25 02:12:49 +00:00
|
|
|
inline Partition
|
2017-03-23 19:16:39 +00:00
|
|
|
make_part(void *memory, i32_4tech size){
|
2016-05-25 02:12:49 +00:00
|
|
|
Partition partition;
|
|
|
|
partition.base = (char*)memory;
|
|
|
|
partition.pos = 0;
|
|
|
|
partition.max = size;
|
|
|
|
return partition;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void*
|
2017-03-23 19:16:39 +00:00
|
|
|
partition_allocate(Partition *data, i32_4tech size){
|
2016-05-25 02:12:49 +00:00
|
|
|
void *ret = 0;
|
|
|
|
if (size > 0 && data->pos + size <= data->max){
|
|
|
|
ret = data->base + data->pos;
|
|
|
|
data->pos += size;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-25 03:42:16 +00:00
|
|
|
inline void
|
|
|
|
partition_reduce(Partition *data, i32_4tech size){
|
|
|
|
if (size > 0 && size <= data->pos){
|
|
|
|
data->pos -= size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 02:12:49 +00:00
|
|
|
inline void
|
2017-03-23 19:16:39 +00:00
|
|
|
partition_align(Partition *data, u32_4tech boundary){
|
2016-05-25 02:12:49 +00:00
|
|
|
--boundary;
|
|
|
|
data->pos = (data->pos + boundary) & (~boundary);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void*
|
|
|
|
partition_current(Partition *data){
|
2016-12-08 17:16:50 +00:00
|
|
|
return(data->base + data->pos);
|
2016-05-25 02:12:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:16:39 +00:00
|
|
|
inline i32_4tech
|
2016-05-25 02:12:49 +00:00
|
|
|
partition_remaining(Partition *data){
|
2016-12-08 17:16:50 +00:00
|
|
|
return(data->max - data->pos);
|
2016-05-25 02:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Partition
|
2017-01-23 06:19:43 +00:00
|
|
|
partition_sub_part(Partition *data, i32_4tech size){
|
2016-05-25 02:12:49 +00:00
|
|
|
Partition result = {};
|
|
|
|
void *d = partition_allocate(data, size);
|
2016-10-27 05:58:22 +00:00
|
|
|
if (d){
|
|
|
|
result = make_part(d, size);
|
|
|
|
}
|
2016-12-08 17:16:50 +00:00
|
|
|
return(result);
|
2016-05-25 02:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define push_struct(part, T) (T*)partition_allocate(part, sizeof(T))
|
|
|
|
#define push_array(part, T, size) (T*)partition_allocate(part, sizeof(T)*(size))
|
|
|
|
#define push_block(part, size) partition_allocate(part, size)
|
|
|
|
|
|
|
|
inline Temp_Memory
|
|
|
|
begin_temp_memory(Partition *data){
|
|
|
|
Temp_Memory result;
|
|
|
|
result.handle = data;
|
|
|
|
result.pos = data->pos;
|
2016-12-08 17:16:50 +00:00
|
|
|
return(result);
|
2016-05-25 02:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
end_temp_memory(Temp_Memory temp){
|
|
|
|
((Partition*)temp.handle)->pos = temp.pos;
|
|
|
|
}
|
|
|
|
|
2016-12-08 17:16:50 +00:00
|
|
|
inline Tail_Temp_Partition
|
2017-03-23 19:16:39 +00:00
|
|
|
begin_tail_part(Partition *data, i32_4tech size){
|
2016-12-08 17:16:50 +00:00
|
|
|
Tail_Temp_Partition result = {0};
|
|
|
|
if (data->pos + size <= data->max){
|
2017-01-14 03:01:35 +00:00
|
|
|
result.handle = data;
|
|
|
|
result.old_max = data->max;
|
2016-12-08 17:16:50 +00:00
|
|
|
data->max -= size;
|
|
|
|
result.part = make_part(data->base + data->max, size);
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
end_tail_part(Tail_Temp_Partition temp){
|
|
|
|
if (temp.handle){
|
|
|
|
Partition *part = (Partition*)temp.handle;
|
|
|
|
part->max = temp.old_max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 05:49:11 +00:00
|
|
|
/*
|
|
|
|
NOTE(allen):
|
|
|
|
This is a very week general purpose allocator system.
|
|
|
|
It should only be used for infrequent large allocations (4K+).
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum{
|
|
|
|
MEM_BUBBLE_FLAG_INIT = 0x0,
|
|
|
|
MEM_BUBBLE_USED = 0x1,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Bubble{
|
|
|
|
Bubble *prev;
|
|
|
|
Bubble *next;
|
2016-07-20 17:19:20 +00:00
|
|
|
Bubble *prev2;
|
|
|
|
Bubble *next2;
|
2017-03-23 19:15:33 +00:00
|
|
|
i32_4tech size;
|
2017-01-23 06:19:43 +00:00
|
|
|
u32_4tech flags;
|
2017-03-23 19:15:33 +00:00
|
|
|
u32_4tech _unused_[4];
|
2016-07-10 05:49:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct General_Memory{
|
|
|
|
Bubble sentinel;
|
2016-07-20 17:19:20 +00:00
|
|
|
Bubble free_sentinel;
|
|
|
|
Bubble used_sentinel;
|
2016-07-10 05:49:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Mem_Options{
|
|
|
|
Partition part;
|
|
|
|
General_Memory general;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void
|
|
|
|
insert_bubble(Bubble *prev, Bubble *bubble){
|
|
|
|
bubble->prev = prev;
|
|
|
|
bubble->next = prev->next;
|
|
|
|
bubble->prev->next = bubble;
|
|
|
|
bubble->next->prev = bubble;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
remove_bubble(Bubble *bubble){
|
|
|
|
bubble->prev->next = bubble->next;
|
|
|
|
bubble->next->prev = bubble->prev;
|
|
|
|
}
|
|
|
|
|
2016-07-20 17:19:20 +00:00
|
|
|
inline void
|
|
|
|
insert_bubble2(Bubble *prev, Bubble *bubble){
|
|
|
|
bubble->prev2 = prev;
|
|
|
|
bubble->next2 = prev->next2;
|
|
|
|
bubble->prev2->next2 = bubble;
|
|
|
|
bubble->next2->prev2 = bubble;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
remove_bubble2(Bubble *bubble){
|
|
|
|
bubble->prev2->next2 = bubble->next2;
|
|
|
|
bubble->next2->prev2 = bubble->prev2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
general_sentinel_init(Bubble *bubble){
|
|
|
|
bubble->prev = bubble;
|
|
|
|
bubble->next = bubble;
|
|
|
|
bubble->prev2 = bubble;
|
|
|
|
bubble->next2 = bubble;
|
|
|
|
bubble->flags = MEM_BUBBLE_USED;
|
|
|
|
bubble->size = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-22 15:30:25 +00:00
|
|
|
#define BUBBLE_MIN_SIZE 1024
|
|
|
|
|
|
|
|
static void
|
2017-03-23 19:15:33 +00:00
|
|
|
general_memory_attempt_split(General_Memory *general, Bubble *bubble, i32_4tech wanted_size){
|
|
|
|
i32_4tech remaining_size = bubble->size - wanted_size;
|
2016-10-22 15:30:25 +00:00
|
|
|
if (remaining_size >= BUBBLE_MIN_SIZE){
|
|
|
|
bubble->size = wanted_size;
|
|
|
|
Bubble *new_bubble = (Bubble*)((char*)(bubble + 1) + wanted_size);
|
2017-01-23 06:19:43 +00:00
|
|
|
new_bubble->flags = (u32_4tech)MEM_BUBBLE_FLAG_INIT;
|
2016-10-22 15:30:25 +00:00
|
|
|
new_bubble->size = remaining_size - sizeof(Bubble);
|
|
|
|
insert_bubble(bubble, new_bubble);
|
|
|
|
insert_bubble2(&general->free_sentinel, new_bubble);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
general_memory_do_merge(Bubble *left, Bubble *right){
|
|
|
|
left->size += sizeof(Bubble) + right->size;
|
|
|
|
remove_bubble(right);
|
|
|
|
remove_bubble2(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
general_memory_attempt_merge(Bubble *left, Bubble *right){
|
|
|
|
if (!(left->flags & MEM_BUBBLE_USED) && !(right->flags & MEM_BUBBLE_USED)){
|
|
|
|
general_memory_do_merge(left, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(allen): public procedures
|
2016-07-10 05:49:11 +00:00
|
|
|
static void
|
2017-01-23 06:19:43 +00:00
|
|
|
general_memory_open(General_Memory *general, void *memory, i32_4tech size){
|
2016-07-20 17:19:20 +00:00
|
|
|
general_sentinel_init(&general->sentinel);
|
|
|
|
general_sentinel_init(&general->free_sentinel);
|
|
|
|
general_sentinel_init(&general->used_sentinel);
|
2016-07-10 05:49:11 +00:00
|
|
|
|
|
|
|
Bubble *first = (Bubble*)memory;
|
2017-01-23 06:19:43 +00:00
|
|
|
first->flags = (u32_4tech)MEM_BUBBLE_FLAG_INIT;
|
2016-07-10 05:49:11 +00:00
|
|
|
first->size = size - sizeof(Bubble);
|
|
|
|
insert_bubble(&general->sentinel, first);
|
2016-07-20 17:19:20 +00:00
|
|
|
insert_bubble2(&general->free_sentinel, first);
|
2016-07-10 05:49:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 03:01:35 +00:00
|
|
|
#if defined(Assert)
|
2017-01-23 06:19:43 +00:00
|
|
|
static i32_4tech
|
2016-07-10 05:49:11 +00:00
|
|
|
general_memory_check(General_Memory *general){
|
|
|
|
Bubble *sentinel = &general->sentinel;
|
|
|
|
for (Bubble *bubble = sentinel->next;
|
|
|
|
bubble != sentinel;
|
|
|
|
bubble = bubble->next){
|
2016-08-30 19:30:41 +00:00
|
|
|
Assert(bubble);
|
2016-07-10 05:49:11 +00:00
|
|
|
|
|
|
|
Bubble *next = bubble->next;
|
2016-08-30 19:30:41 +00:00
|
|
|
Assert(bubble == next->prev);
|
2016-07-10 05:49:11 +00:00
|
|
|
if (next != sentinel && bubble->prev != sentinel){
|
2016-08-30 19:30:41 +00:00
|
|
|
Assert(bubble->next > bubble);
|
|
|
|
Assert(bubble > bubble->prev);
|
2016-07-10 05:49:11 +00:00
|
|
|
|
|
|
|
char *end_ptr = (char*)(bubble + 1) + bubble->size;
|
|
|
|
char *next_ptr = (char*)next;
|
|
|
|
(void)(end_ptr);
|
|
|
|
(void)(next_ptr);
|
2016-08-30 19:30:41 +00:00
|
|
|
Assert(end_ptr == next_ptr);
|
2016-07-10 05:49:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(1);
|
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
#else
|
2017-01-23 06:19:43 +00:00
|
|
|
static i32_4tech
|
2016-08-30 19:30:41 +00:00
|
|
|
general_memory_check(General_Memory *general){}
|
|
|
|
#endif
|
2016-07-10 05:49:11 +00:00
|
|
|
|
|
|
|
static void*
|
2017-03-23 19:15:33 +00:00
|
|
|
general_memory_allocate(General_Memory *general, i32_4tech size){
|
2016-07-10 05:49:11 +00:00
|
|
|
void *result = 0;
|
2016-07-20 17:19:20 +00:00
|
|
|
if (size < BUBBLE_MIN_SIZE) size = BUBBLE_MIN_SIZE;
|
2016-07-20 19:25:50 +00:00
|
|
|
for (Bubble *bubble = general->free_sentinel.next2;
|
|
|
|
bubble != &general->free_sentinel;
|
|
|
|
bubble = bubble->next2){
|
2016-07-10 05:49:11 +00:00
|
|
|
if (!(bubble->flags & MEM_BUBBLE_USED)){
|
|
|
|
if (bubble->size >= size){
|
|
|
|
result = bubble + 1;
|
|
|
|
bubble->flags |= MEM_BUBBLE_USED;
|
2016-07-20 17:19:20 +00:00
|
|
|
remove_bubble2(bubble);
|
|
|
|
insert_bubble2(&general->used_sentinel, bubble);
|
|
|
|
general_memory_attempt_split(general, bubble, size);
|
2016-07-10 05:49:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 17:19:20 +00:00
|
|
|
return(result);
|
2016-07-10 05:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
general_memory_free(General_Memory *general, void *memory){
|
|
|
|
Bubble *bubble = ((Bubble*)memory) - 1;
|
2016-07-20 17:19:20 +00:00
|
|
|
bubble->flags = 0;
|
|
|
|
|
|
|
|
remove_bubble2(bubble);
|
|
|
|
insert_bubble2(&general->free_sentinel, bubble);
|
|
|
|
|
2016-07-10 05:49:11 +00:00
|
|
|
Bubble *prev, *next;
|
|
|
|
prev = bubble->prev;
|
|
|
|
next = bubble->next;
|
|
|
|
general_memory_attempt_merge(bubble, next);
|
|
|
|
general_memory_attempt_merge(prev, bubble);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
2017-03-23 19:15:33 +00:00
|
|
|
general_memory_reallocate(General_Memory *general, void *old, i32_4tech old_size, i32_4tech size){
|
2016-07-10 05:49:11 +00:00
|
|
|
void *result = old;
|
|
|
|
Bubble *bubble = ((Bubble*)old) - 1;
|
2017-03-23 19:15:33 +00:00
|
|
|
i32_4tech additional_space = size - bubble->size;
|
2016-07-10 05:49:11 +00:00
|
|
|
if (additional_space > 0){
|
|
|
|
Bubble *next = bubble->next;
|
|
|
|
if (!(next->flags & MEM_BUBBLE_USED) &&
|
2017-02-25 01:53:40 +00:00
|
|
|
next->size + (i32_4tech)sizeof(Bubble) >= additional_space){
|
2016-07-10 05:49:11 +00:00
|
|
|
general_memory_do_merge(bubble, next);
|
2016-07-20 17:19:20 +00:00
|
|
|
general_memory_attempt_split(general, bubble, size);
|
2016-07-10 05:49:11 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
result = general_memory_allocate(general, size);
|
|
|
|
if (old_size) memcpy(result, old, old_size);
|
|
|
|
general_memory_free(general, old);
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 17:19:20 +00:00
|
|
|
return(result);
|
2016-07-10 05:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void*
|
2017-03-23 19:15:33 +00:00
|
|
|
general_memory_reallocate_nocopy(General_Memory *general, void *old, i32_4tech size){
|
2016-07-20 17:19:20 +00:00
|
|
|
void *result = general_memory_reallocate(general, old, 0, size);
|
|
|
|
return(result);
|
2016-07-10 05:49:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 03:01:35 +00:00
|
|
|
#define reset_temp_memory end_temp_memory
|
|
|
|
#define gen_struct(g, T) (T*)general_memory_allocate(g, sizeof(T), 0)
|
|
|
|
#define gen_array(g, T, size) (T*)general_memory_allocate(g, sizeof(T)*(size))
|
|
|
|
#define gen_block(g, size) general_memory_open(g, size, 0)
|
|
|
|
|
2016-06-07 20:12:23 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-18 01:04:41 +00:00
|
|
|
// BOTTOM
|
|
|
|
|