-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.h
69 lines (55 loc) · 1.95 KB
/
memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef MEMORY_H
#define MEMORY_H
#include <stdlib.h>
#include <pthread.h>
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define DEBUG_MEMORY
typedef struct FixedAllocator_ {
#ifdef DEBUG_MEMORY
const char* name;
long inflight;
long max_inflight;
#endif
pthread_mutex_t mutex;
size_t allocation_size;
void* first_free;
} *FixedAllocator;
typedef struct StackAllocator_ {
#ifdef DEBUG_MEMORY
const char* name;
#endif
pthread_mutex_t mutex;
void* stack_top;
void* stack_bottom;
void* stack_max;
} *StackAllocator;
FixedAllocator fixed_allocator_make(size_t obj_size, unsigned int n,
const char* name);
void* fixed_allocator_alloc(FixedAllocator allocator);
void fixed_allocator_free(FixedAllocator allocator, void *obj);
StackAllocator stack_allocator_make(size_t stack_size,
const char* name);
void* stack_allocator_alloc(StackAllocator allocator, size_t size);
void stack_allocator_freeall(StackAllocator allocator);
typedef struct CircularBuffer_ {
int read_index;
int write_index;
int size;
int filled;
char* data;
} *CircularBuffer;
CircularBuffer circularbuffer_make(size_t bytes);
void circularbuffer_free(CircularBuffer buffer);
int circularbuffer_bytes_writable(CircularBuffer buffer);
int circularbuffer_bytes_readable(CircularBuffer buffer);
void circularbuffer_read_buffers(CircularBuffer buffer,
char ** buffer1, int * size1,
char ** buffer2, int * size2,
int bytes_to_read);
void circularbuffer_write_buffers(CircularBuffer buffer,
char ** buffer1, int * size1,
char ** buffer2, int * size2,
int bytes_to_write);
int circularbuffer_insert(CircularBuffer buffer, char * bytes, int length);
int circularbuffer_read(CircularBuffer buffer, char * target, int length);
#endif