-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshmalloc.h
53 lines (42 loc) · 1.23 KB
/
shmalloc.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
#ifndef SHMALLOC_H
#define SHMALLOC_H
#include <pthread.h>
#define BITSEQ 536870911
#define shmalloc(i, s, p, sz) _shmalloc(i, s, p, sz, __FILE__, __LINE__)
#define shmfree(ptr, sz, s) _shmfree(ptr, sz, s, __FILE__, __LINE__)
/**
* A header for managing memory.
*/
struct Header {
int bitseq;
int id;
int refcount;
size_t size;
long prev, next; // offsets
unsigned char has_mutex;
unsigned char is_free;
pthread_mutex_t mutex;
pthread_mutexattr_t attr;
};
typedef struct Header Header;
/**
* Initializes values in header
*/
void initialize_header(Header *h, size_t size, int id, unsigned char is_first);
/**
* Destroys the given header structure.
*/
void destroy_header(Header *, void *);
/**
* Allocate shared .memory that's already been attached via shmat(3).
* Returns a pointer to the newly allocated memory.
*/
void *_shmalloc(int id, size_t *size, void *shmptr, size_t shm_size,
char *filename, int linenumber);
/**
* Frees a block of shared memory previously allocated with shmalloc().
*/
void _shmfree(void *ptr, size_t shm_size, void *shm_ptr, char *filename, int linenumber);
long ptr2offset(void *ptr, void *shm_ptr);
void *offset2ptr(long offset, void *shm_ptr);
#endif