summaryrefslogtreecommitdiff
path: root/src/lib/eo/eo_ptr_indirection.c
diff options
context:
space:
mode:
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>2013-04-23 23:38:44 +0900
committerCarsten Haitzler (Rasterman) <raster@rasterman.com>2013-04-23 23:39:37 +0900
commit4cf68bf728b5a858ce1c0efac25ded1a49ba8d9d (patch)
tree32f0e72c75b9885eb6d0ecfc7088aeaca984db03 /src/lib/eo/eo_ptr_indirection.c
parentc9ad644be22e5174c84d7108e415dbc8ad0c6422 (diff)
eoid -> use mmap for allocating id tables. makes it a separate memory
regions with page separations... this allows us more direct control over access and visibility.
Diffstat (limited to 'src/lib/eo/eo_ptr_indirection.c')
-rw-r--r--src/lib/eo/eo_ptr_indirection.c76
1 files changed, 72 insertions, 4 deletions
diff --git a/src/lib/eo/eo_ptr_indirection.c b/src/lib/eo/eo_ptr_indirection.c
index 19ca52b..19dc17b 100644
--- a/src/lib/eo/eo_ptr_indirection.c
+++ b/src/lib/eo/eo_ptr_indirection.c
@@ -3,6 +3,12 @@
3#endif 3#endif
4 4
5#include "eo_ptr_indirection.h" 5#include "eo_ptr_indirection.h"
6#ifdef __linux__
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <sys/mman.h>
11#endif
6 12
7/* Start of pointer indirection: 13/* Start of pointer indirection:
8 * 14 *
@@ -74,6 +80,68 @@ typedef uintptr_t Table_Index;
74#define MAX_IDS_PER_TABLE (1 << BITS_FOR_ID_IN_TABLE) 80#define MAX_IDS_PER_TABLE (1 << BITS_FOR_ID_IN_TABLE)
75#define MAX_GENERATIONS (1 << BITS_FOR_GENERATION_COUNTER) 81#define MAX_GENERATIONS (1 << BITS_FOR_GENERATION_COUNTER)
76 82
83#define MEM_HEADER_SIZE 16
84#define MEM_PAGE_SIZE 4096
85#define MEM_MAGIC 0x3f61ec8a
86
87typedef struct _Mem_Header
88{
89 size_t size;
90 size_t magic;
91} Mem_Header;
92
93static void *
94_eo_id_mem_alloc(size_t size)
95{
96#ifdef __linux__
97 void *ptr;
98 Mem_Header *hdr;
99 size_t newsize;
100 newsize = MEM_PAGE_SIZE * ((size + MEM_HEADER_SIZE + MEM_PAGE_SIZE - 1) /
101 MEM_PAGE_SIZE);
102 ptr = mmap(NULL, newsize, PROT_READ | PROT_WRITE,
103 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
104 if (ptr == MAP_FAILED)
105 {
106 ERR("mmap of eo id table region failed!");
107 return NULL;
108 }
109 hdr = ptr;
110 hdr->size = size;
111 hdr->magic = MEM_MAGIC;
112 return (void *)(((unsigned char *)ptr) + MEM_HEADER_SIZE);
113#else
114 return malloc(size);
115#endif
116}
117
118static void *
119_eo_id_mem_calloc(size_t num, size_t size)
120{
121 void *ptr = _eo_id_mem_alloc(num * size);
122 if (!ptr) return NULL;
123 memset(ptr, 0, num * size);
124 return ptr;
125}
126
127static void
128_eo_id_mem_free(void *ptr)
129{
130#ifdef __linux__
131 Mem_Header *hdr;
132 if (!ptr) return;
133 hdr = (Mem_Header *)(((unsigned char *)ptr) - MEM_HEADER_SIZE);
134 if (hdr->magic != MEM_MAGIC)
135 {
136 ERR("unmap of eo table region has bad magic!");
137 return;
138 }
139 munmap(hdr, hdr->size);
140#else
141 free(ptr);
142#endif
143}
144
77/* Table */ 145/* Table */
78typedef struct 146typedef struct
79{ 147{
@@ -158,7 +226,7 @@ _eo_id_allocate(const _Eo *obj)
158 if (!_eo_ids_tables[table_id]) 226 if (!_eo_ids_tables[table_id])
159 { 227 {
160 /* We allocate a new table */ 228 /* We allocate a new table */
161 _eo_ids_tables[table_id] = calloc(MAX_IDS_INTER_TABLES, sizeof(_Eo_Ids_Table*)); 229 _eo_ids_tables[table_id] = _eo_id_mem_calloc(MAX_IDS_INTER_TABLES, sizeof(_Eo_Ids_Table*));
162 } 230 }
163 for (Table_Index int_table_id = 0; int_table_id < MAX_IDS_INTER_TABLES; int_table_id++) 231 for (Table_Index int_table_id = 0; int_table_id < MAX_IDS_INTER_TABLES; int_table_id++)
164 { 232 {
@@ -166,7 +234,7 @@ _eo_id_allocate(const _Eo *obj)
166 if (!ID_TABLE) 234 if (!ID_TABLE)
167 { 235 {
168 /* We allocate a new intermediate table */ 236 /* We allocate a new intermediate table */
169 ID_TABLE = calloc(1, sizeof(_Eo_Ids_Table)); 237 ID_TABLE = _eo_id_mem_calloc(1, sizeof(_Eo_Ids_Table));
170 eina_trash_init(&(ID_TABLE->queue)); 238 eina_trash_init(&(ID_TABLE->queue));
171 /* We select directly the first entry of the new table */ 239 /* We select directly the first entry of the new table */
172 ptr = &(ID_TABLE->ptrs[0]); 240 ptr = &(ID_TABLE->ptrs[0]);
@@ -242,10 +310,10 @@ _eo_free_ids_tables()
242 { 310 {
243 if (ID_TABLE) 311 if (ID_TABLE)
244 { 312 {
245 free(ID_TABLE); 313 _eo_id_mem_free(ID_TABLE);
246 } 314 }
247 } 315 }
248 free(_eo_ids_tables[table_id]); 316 _eo_id_mem_free(_eo_ids_tables[table_id]);
249 } 317 }
250 _eo_ids_tables[table_id] = NULL; 318 _eo_ids_tables[table_id] = NULL;
251 } 319 }