-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmem.c
1032 lines (884 loc) · 31.2 KB
/
mem.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2004-2008 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include "ci_threads.h"
#include "debug.h"
#include "mem.h"
#include "stats.h"
#include <assert.h>
int ci_buffers_init();
/*General Functions */
static int memory_pools_stat_group = -1;
const char *MEMPOOLS_STAT_MASTER_GROUP = "Memory Pools";
ci_mem_allocator_t *default_allocator = NULL;
static int MEM_ALLOCATOR_POOL = -1;
static int PACK_ALLOCATOR_POOL = -1;
static size_t sizeof_pack_allocator();
ci_mem_allocator_t *ci_create_pool_allocator(const char *name, int items_size);
int ci_mem_init()
{
int ret = -1;
memory_pools_stat_group = ci_stat_mastergroup_register(MEMPOOLS_STAT_MASTER_GROUP);
ret = ci_buffers_init();
default_allocator = ci_create_os_allocator();
if (!default_allocator && ret ==-1)
ret = 0;
MEM_ALLOCATOR_POOL = ci_object_pool_register("ci_mem_allocator_t", sizeof(ci_mem_allocator_t));
assert(MEM_ALLOCATOR_POOL >= 0);
PACK_ALLOCATOR_POOL = ci_object_pool_register("pack_allocator_t", sizeof_pack_allocator());
assert(PACK_ALLOCATOR_POOL >= 0);
return ret;
}
void ci_mem_reset()
{
}
void ci_object_pools_destroy();
void ci_mem_exit()
{
ci_mem_allocator_destroy(default_allocator);
default_allocator = NULL;
ci_buffers_destroy();
MEM_ALLOCATOR_POOL = -1;
PACK_ALLOCATOR_POOL = -1;
ci_object_pools_destroy();
}
void ci_mem_allocator_destroy(ci_mem_allocator_t *allocator)
{
if (allocator->name) {
free(allocator->name);
allocator->name = NULL;
}
/* The allocator->destroy may release allocator struct */
int must_free = allocator->must_free;
void (*destroyer)(struct ci_mem_allocator *);
destroyer = allocator->destroy;
destroyer(allocator);
/*space for ci_mem_allocator_t struct is not always allocated
using malloc */
if (must_free == 1)
free(allocator);
else if (must_free == 2)
ci_object_pool_free(allocator);
/*
else if (allocator->must_free == 0) {
user is responsible to release the allocator object
or the object is already released while the
destroyer/allocator->destroy is called.
}
*/
}
/******************/
static ci_mem_allocator_t *alloc_mem_allocator_struct()
{
ci_mem_allocator_t *alc;
if (MEM_ALLOCATOR_POOL < 0) {
alc = (ci_mem_allocator_t *) malloc(sizeof(ci_mem_allocator_t));
alc->must_free = 1;
} else {
alc = ci_object_pool_alloc(MEM_ALLOCATOR_POOL);
alc->must_free = 2;
}
return alc;
}
/*******************************************************************/
/* Buffers pool api functions */
#define BUF_SIGNATURE 0xAA55
struct mem_buffer_block {
uint16_t sig;
size_t ID;
union {
double __align;
char ptr[1];
} data;
};
#if !defined(offsetof)
#define offsetof(type,member) ((size_t) &((type*)0)->member)
#endif
#define PTR_OFFSET offsetof(struct mem_buffer_block,data.ptr[0])
static ci_mem_allocator_t *short_buffers[16];
static ci_mem_allocator_t *long_buffers[16];
enum {
BUF64_POOL, BUF128_POOL, BUF256_POOL,BUF512_POOL, BUF1024_POOL,
BUF2048_POOL, BUF4096_POOL, BUF8192_POOL, BUF16384_POOL, BUF32768_POOL,
BUF_END_POOL
};
static ci_mem_allocator_t *Pools[BUF_END_POOL];
int ci_buffers_init()
{
int i;
memset(Pools, 0, sizeof(Pools));
memset(short_buffers, 0, sizeof(short_buffers));
memset(long_buffers, 0, sizeof(long_buffers));
Pools[BUF64_POOL] = ci_create_pool_allocator("64bytes", 64+PTR_OFFSET);
Pools[BUF128_POOL] = ci_create_pool_allocator("128bytes", 128+PTR_OFFSET);
Pools[BUF256_POOL] = ci_create_pool_allocator("256bytes", 256+PTR_OFFSET);
Pools[BUF512_POOL] = ci_create_pool_allocator("512bytes", 512+PTR_OFFSET);
Pools[BUF1024_POOL] = ci_create_pool_allocator("1Kb", 1024+PTR_OFFSET);
Pools[BUF2048_POOL] = ci_create_pool_allocator("2Kb", 2048+PTR_OFFSET);
Pools[BUF4096_POOL] = ci_create_pool_allocator("4Kb", 4096+PTR_OFFSET);
Pools[BUF8192_POOL] = ci_create_pool_allocator("8Kb", 8192+PTR_OFFSET);
Pools[BUF16384_POOL] = ci_create_pool_allocator("16Kb", 16384+PTR_OFFSET);
Pools[BUF32768_POOL] = ci_create_pool_allocator("32Kb", 32768+PTR_OFFSET);
short_buffers[0] = Pools[BUF64_POOL];
short_buffers[1] = Pools[BUF128_POOL];
short_buffers[2] = short_buffers[3] = Pools[BUF256_POOL];
short_buffers[4] = short_buffers[5] =
short_buffers[6] = short_buffers[7] = Pools[BUF512_POOL];
for (i = 8; i < 16; i++)
short_buffers[i] = Pools[BUF1024_POOL];
long_buffers[0] = Pools[BUF2048_POOL];
long_buffers[1] = Pools[BUF4096_POOL];
long_buffers[2] = long_buffers[3] = Pools[BUF8192_POOL];
long_buffers[4] = long_buffers[5] =
long_buffers[6] = long_buffers[7] = Pools[BUF16384_POOL];
for (i = 8; i < 16; i++)
long_buffers[i] = Pools[BUF32768_POOL];
return 1;
}
static int short_buffer_sizes[16] = {
64,
128,
256,256,
512, 512, 512, 512,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024
};
static int long_buffer_sizes[16] = {
2048,
4096,
8192, 8192,
16384, 16384, 16384, 16384,
32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768
};
void ci_buffers_destroy()
{
int i;
for (i = 0; i < BUF_END_POOL; i++) {
if (Pools[i] != NULL)
ci_mem_allocator_destroy(Pools[i]);
}
memset(Pools, 0, sizeof(Pools));
memset(short_buffers, 0, sizeof(short_buffers));
memset(long_buffers, 0, sizeof(long_buffers));
}
void *ci_buffer_alloc2(size_t block_size, size_t *allocated_size)
{
int type;
size_t mem_size, allocated_buffer_size;
struct mem_buffer_block *block = NULL;
mem_size = block_size + PTR_OFFSET;
type = (block_size-1) >> 6;
if (type < 16) {
assert(short_buffers[type] != NULL);
block = short_buffers[type]->alloc(short_buffers[type], mem_size);
allocated_buffer_size = short_buffer_sizes[type];
} else if (type < 512) {
int long_sub_type = type >> 5;
assert(long_buffers[long_sub_type] != NULL);
block = long_buffers[long_sub_type]->alloc(long_buffers[long_sub_type], mem_size);
allocated_buffer_size = long_buffer_sizes[long_sub_type];
} else {
block = (struct mem_buffer_block *)malloc(mem_size);
allocated_buffer_size = block_size;
}
if (!block) {
ci_debug_printf(1, "Failed to allocate space for buffer of size:%d\n", (int)block_size);
return NULL;
}
block->sig = BUF_SIGNATURE;
if (allocated_size) {
*allocated_size = allocated_buffer_size;
block->ID = allocated_buffer_size;
} else
block->ID = block_size;
ci_debug_printf(9, "Requested size %d, getting buffer %p from pool %d:%d\n", (int)block_size, (void *)block->data.ptr, type, (int)allocated_buffer_size);
return (void *)block->data.ptr;
}
void *ci_buffer_alloc(size_t block_size)
{
return ci_buffer_alloc2(block_size, NULL);
}
static struct mem_buffer_block *to_block(const void *data)
{
struct mem_buffer_block *block;
block = (struct mem_buffer_block *)(((char *)data) - PTR_OFFSET);
if (block->sig != BUF_SIGNATURE) {
ci_debug_printf(1,"ci_buffer internal check: ERROR, %p is not a ci_buffer object. This is a bug!!!!\n", data);
return NULL;
}
return block;
}
CI_DECLARE_FUNC(int) ci_buffer_check(const void *data)
{
return to_block(data) ? 1 : 0;
}
CI_DECLARE_FUNC(size_t) ci_buffer_size(const void *data)
{
const struct mem_buffer_block *block = to_block(data);
return block ? block->ID : 0;
}
static size_t ci_buffer_real_size(const void *data)
{
const struct mem_buffer_block *block = to_block(data);
if (!block)
return 0;
int type;
size_t buffer_block_size = 0;
type = (block->ID - 1) >> 6;
if (type < 16) {
assert(short_buffers[type] != NULL);
buffer_block_size = short_buffer_sizes[type];
} else if (type < 512) {
type = type >> 5;
assert(long_buffers[type] != NULL);
buffer_block_size = long_buffer_sizes[type];
} else
buffer_block_size = block->ID;
return buffer_block_size;
}
void * ci_buffer_realloc2(void *data, size_t new_block_size, size_t *allocated_size)
{
if (!data)
return ci_buffer_alloc2(new_block_size, allocated_size);
size_t current_buffer_size = 0;
struct mem_buffer_block *block;
if (!(block = to_block(data)))
return NULL;
current_buffer_size = ci_buffer_real_size(data);
assert(current_buffer_size > 0);
ci_debug_printf(9, "Current buffer %p of size for realloc: %d, requested block size: %d. The initial size: %d\n",
data,
(int)current_buffer_size, (int)new_block_size, (int)block->ID);
/*If no block_size created than our buffer actual size probably requires a realloc.....*/
if (new_block_size > current_buffer_size) {
data = ci_buffer_alloc2(new_block_size, allocated_size);
if (!data)
return NULL;
memcpy(data, block->data.ptr, block->ID);
ci_buffer_free(block->data.ptr);
} else {
/*we neeed to update block->ID to the new requested size...*/
if (allocated_size) {
*allocated_size = current_buffer_size;
block->ID = current_buffer_size;
} else
block->ID = new_block_size;
}
ci_debug_printf(9, "New memory buffer %p of size %d, actual reserved buffer of size: %d\n", data, (int) new_block_size, (int)ci_buffer_real_size(data));
return data;
}
void * ci_buffer_realloc(void *data, size_t block_size)
{
return ci_buffer_realloc2(data, block_size, NULL);
}
void ci_buffer_free(void *data)
{
int type;
size_t block_size;
struct mem_buffer_block *block;
if (!data)
return;
if (!(block = to_block(data)))
return;
block_size = block->ID;
type = (block_size-1) >> 6;
if (type < 16) {
assert(short_buffers[type] != NULL);
short_buffers[type]->free(short_buffers[type], block);
ci_debug_printf(9, "Store buffer %p (used %d bytes) to short pool %d:%d\n", data, (int)block_size, type, short_buffer_sizes[type]);
} else if (type < 512) {
int long_sub_type = type >> 5;
assert(long_buffers[long_sub_type] != NULL);
long_buffers[long_sub_type]->free(long_buffers[long_sub_type], block);
ci_debug_printf(9, "Store buffer %p (used %d bytes) to long pool %d:%d\n", data, (int)block_size, type, long_buffer_sizes[long_sub_type]);
} else {
ci_debug_printf(9, "Free buffer %p (free at %p, used %d bytes)\n", data, block, (int)block->ID);
free(block);
}
}
/*******************************************************************/
/*Object pools */
#define OBJ_SIGNATURE 0x55AA
ci_mem_allocator_t **object_pools = NULL;
unsigned long object_pools_size = 0;
unsigned long object_pools_used = 0;
int ci_object_pools_init()
{
return 1;
}
void ci_object_pools_destroy()
{
unsigned int i;
for (i = 0; i < object_pools_used; i++) {
if (object_pools[i] != NULL)
ci_mem_allocator_destroy(object_pools[i]);
}
}
#define STEP 128
int ci_object_pool_register(const char *name, int size)
{
int ID, i;
ID = -1;
/*search for an empty position on object_pools and assign here?*/
if (object_pools == NULL) {
object_pools = malloc(STEP*sizeof(ci_mem_allocator_t *));
object_pools_size = STEP;
ID = 0;
} else {
for (i = 0; i < object_pools_used; i++) {
if (object_pools[i] == NULL) {
ID = i;
break;
}
}
if (ID == -1) {
if (object_pools_size == object_pools_used) {
object_pools_size += STEP;
object_pools = realloc(object_pools, object_pools_size*sizeof(ci_mem_allocator_t *));
}
ID=object_pools_used;
}
}
if (object_pools == NULL) //??????
return -1;
object_pools[ID] = ci_create_pool_allocator(name, size+PTR_OFFSET);
object_pools_used++;
return ID;
}
void ci_object_pool_unregister(int id)
{
if (id >= object_pools_used || id < 0) {
/*A error message ....*/
return;
}
if (object_pools[id]) {
ci_mem_allocator_destroy(object_pools[id]);
object_pools[id] = NULL;
}
}
void *ci_object_pool_alloc(int id)
{
struct mem_buffer_block *block = NULL;
if (id >= object_pools_used || id < 0 || !object_pools[id]) {
/*A error message ....*/
ci_debug_printf(1, "Invalid object pool %d. This is a BUG!\n", id);
return NULL;
}
block = object_pools[id]->alloc(object_pools[id], 1/*A small size smaller than obj size*/);
if (!block) {
ci_debug_printf(2, "Failed to allocate object from pool %d\n", id);
return NULL;
}
ci_debug_printf(8, "Allocating from objects pool object %d\n", id);
block->sig = OBJ_SIGNATURE;
block->ID = id;
return (void *)block->data.ptr;
}
void ci_object_pool_free(void *ptr)
{
struct mem_buffer_block *block = (struct mem_buffer_block *)((char *)ptr - PTR_OFFSET);
if (block->sig != OBJ_SIGNATURE) {
ci_debug_printf(1,"ci_object_pool_free: ERROR, %p is not internal buffer. This is a bug!!!!\n", ptr);
return;
}
if ((unsigned long)block->ID > object_pools_used || !object_pools[block->ID]) {
ci_debug_printf(1,"ci_object_pool_free: ERROR, %p is pointing to corrupted mem? This is a bug!!!!\n", ptr);
return;
}
ci_debug_printf(8, "Storing to objects pool object %d\n", (int)block->ID);
object_pools[block->ID]->free(object_pools[block->ID], block);
}
/*******************************************************************/
/*A simple allocator implementation which uses the system malloc */
static void *os_allocator_alloc(ci_mem_allocator_t *allocator,size_t size)
{
return malloc(size);
}
static void os_allocator_free(ci_mem_allocator_t *allocator,void *p)
{
free(p);
}
static void os_allocator_reset(ci_mem_allocator_t *allocator)
{
/*nothing to do*/
}
static void os_allocator_destroy(ci_mem_allocator_t *allocator)
{
/*nothing to do*/
}
ci_mem_allocator_t *ci_create_os_allocator()
{
ci_mem_allocator_t *allocator = alloc_mem_allocator_struct();
if (!allocator)
return NULL;
allocator->alloc = os_allocator_alloc;
allocator->free = os_allocator_free;
allocator->reset = os_allocator_reset;
allocator->destroy = os_allocator_destroy;
allocator->data = NULL;
allocator->name = NULL;
allocator->type = OS_ALLOC;
return allocator;
}
/*Static declaration of an os allocator*/
ci_mem_allocator_t os_allocator_local = {
os_allocator_alloc,
os_allocator_free,
os_allocator_reset,
os_allocator_destroy,
NULL,
"ci_os_allocator",
OS_ALLOC,
0 /*must_free*/
};
/*
The ci_mem_allocator objects can not be const, because their operations
may modify their self.
TODO: check how they can be const
*/
ci_mem_allocator_t *ci_os_allocator = &os_allocator_local;
/************************************************************/
/* The serial allocator implementation */
typedef struct serial_allocator {
char *memchunk;
char *curpos;
char *endpos;
struct serial_allocator *next;
} serial_allocator_t;
static serial_allocator_t *serial_allocator_build(size_t size)
{
serial_allocator_t *serial_alloc;
char *buffer;
size = _CI_ALIGN(size);
/*The serial_allocator and mem_allocator structures will be
allocated in the buffer */
if (size < sizeof(serial_allocator_t) + sizeof(ci_mem_allocator_t))
return NULL;
/*The allocated block size maybe is larger, than the requested.
Fix size to actual block size */
buffer = (char *)ci_buffer_alloc2(size, &size);
serial_alloc = (serial_allocator_t *)buffer;
serial_alloc->memchunk = buffer + sizeof(serial_allocator_t);
size -= sizeof(serial_allocator_t);
serial_alloc->curpos = serial_alloc->memchunk;
serial_alloc->endpos = serial_alloc->memchunk + size;
serial_alloc->next = NULL;
return serial_alloc;
}
static void *serial_allocation(serial_allocator_t *serial_alloc, size_t size)
{
size_t max_size;
char *mem;
size = _CI_ALIGN(size); /*round size to a correct alignment size*/
max_size = serial_alloc->endpos - serial_alloc->memchunk;
if (size > max_size)
return NULL;
while (size > (serial_alloc->endpos - serial_alloc->curpos)) {
if (serial_alloc->next == NULL) {
serial_alloc->next = serial_allocator_build(max_size);
if (!serial_alloc->next)
return NULL;
}
serial_alloc = serial_alloc->next;
}
mem = serial_alloc->curpos;
serial_alloc->curpos += size;
return (void *)mem;
}
static void *serial_allocator_alloc(ci_mem_allocator_t *allocator,size_t size)
{
serial_allocator_t *serial_alloc = (serial_allocator_t *)allocator->data;
if (!serial_alloc)
return NULL;
return serial_allocation(serial_alloc, size);
}
static void serial_allocator_free(ci_mem_allocator_t *allocator,void *p)
{
/* We can not free :-) */
}
static void serial_allocator_reset(ci_mem_allocator_t *allocator)
{
serial_allocator_t *serial_alloc, *sa;
void *tmp;
serial_alloc = (serial_allocator_t *)allocator->data;
serial_alloc->curpos = serial_alloc->memchunk + _CI_ALIGN(sizeof(ci_mem_allocator_t));
sa = serial_alloc->next;
serial_alloc->next = NULL;
/*release any other allocated chunk*/
while (sa) {
tmp = (void *)sa;
sa = sa->next;
ci_buffer_free(tmp);
}
}
static void serial_allocator_destroy(ci_mem_allocator_t *allocator)
{
serial_allocator_t *sa;
if (!allocator->data)
return;
void *tmp;
sa = (serial_allocator_t *)allocator->data;
while (sa) {
tmp = (void *)sa;
sa= sa->next;
ci_buffer_free(tmp);
}
}
ci_mem_allocator_t *ci_create_serial_allocator(size_t size)
{
ci_mem_allocator_t *allocator;
serial_allocator_t *sdata= serial_allocator_build(size);
/*Allocate space for ci_mem_allocator_t from our serial allocator ...*/
allocator = serial_allocation(sdata, sizeof(ci_mem_allocator_t));
if (!allocator) {
ci_buffer_free((void *)sdata);
return NULL;
}
allocator->alloc = serial_allocator_alloc;
allocator->free = serial_allocator_free;
allocator->reset = serial_allocator_reset;
allocator->destroy = serial_allocator_destroy;
allocator->data = sdata;
allocator->name = NULL;
allocator->type = SERIAL_ALLOC;
/*It is allocated in our buffer space...*/
allocator->must_free = 0;
return allocator;
}
/****************************************************************/
typedef struct pack_allocator {
char *memchunk;
char *curpos;
char *endpos;
char *end;
int must_free;
} pack_allocator_t;
/*Api functions for pack allocator:*/
void *ci_pack_allocator_alloc_unaligned(ci_mem_allocator_t *allocator, size_t size)
{
int max_size;
char *mem;
pack_allocator_t *pack_alloc;
assert(allocator->type == PACK_ALLOC);
pack_alloc = (pack_allocator_t *)allocator->data;
if (!pack_alloc)
return NULL;
max_size = pack_alloc->endpos - pack_alloc->curpos;
if (size > max_size)
return NULL;
mem = pack_alloc->curpos;
pack_alloc->curpos += size;
return (void *)mem;
}
void *ci_pack_allocator_alloc(ci_mem_allocator_t *allocator,size_t size)
{
size = _CI_ALIGN(size); /*round size to a correct alignment size*/
return ci_pack_allocator_alloc_unaligned(allocator, size);
}
void *ci_pack_allocator_alloc_from_rear2(ci_mem_allocator_t *allocator, int size, int align)
{
int max_size;
char *mem;
pack_allocator_t *pack_alloc;
assert(allocator->type == PACK_ALLOC);
pack_alloc = (pack_allocator_t *)allocator->data;
if (!pack_alloc)
return NULL;
if (align)
size = _CI_ALIGN(size); /*round size to a correct alignment size*/
max_size = pack_alloc->endpos - pack_alloc->curpos;
if (size > max_size)
return NULL;
pack_alloc->endpos -= size; /*Allocate block from the end of memory block*/
mem = pack_alloc->endpos;
return (void *)mem;
}
void *ci_pack_allocator_alloc_from_rear(ci_mem_allocator_t *allocator, int size)
{
return ci_pack_allocator_alloc_from_rear2(allocator, size, 1);
}
void *ci_pack_allocator_alloc_from_rear_unaligned(ci_mem_allocator_t *allocator, int size)
{
return ci_pack_allocator_alloc_from_rear2(allocator, size, 0);
}
void ci_pack_allocator_free(ci_mem_allocator_t *allocator,void *p)
{
/* We can not free :-) */
}
void ci_pack_allocator_reset(ci_mem_allocator_t *allocator)
{
pack_allocator_t *pack_alloc;
assert(allocator->type == PACK_ALLOC);
pack_alloc = (pack_allocator_t *)allocator->data;
pack_alloc->curpos = pack_alloc->memchunk;
pack_alloc->endpos = pack_alloc->end;
}
void ci_pack_allocator_destroy(ci_mem_allocator_t *allocator)
{
pack_allocator_t *pack_alloc;
assert(allocator->type == PACK_ALLOC);
pack_alloc = (pack_allocator_t *)allocator->data;
if (pack_alloc->must_free != 0) {
ci_object_pool_free(allocator->data);
allocator->data = NULL;
}
}
/*If "off" is not aligned return the first smaller aligned offset*/
#define _ALIGNED_OFFSET(off) (off != _CI_ALIGN(off) ? _CI_ALIGN(off - _CI_NBYTES_ALIGNMENT) : off)
ci_mem_allocator_t *init_pack_allocator(ci_mem_allocator_t *allocator, pack_allocator_t *pack_alloc, char *memblock, size_t size, int free)
{
/*We may not be able to use all of the memblock size.
We need to support allocating memory space from the end, so we
need to have aligned the pack_alloc->end to correctly calculate
memory block offsets from the end in ci_pack_allocator_alloc_from_rear
function.
*/
size = _ALIGNED_OFFSET(size);
pack_alloc->memchunk = memblock;
pack_alloc->curpos =pack_alloc->memchunk;
pack_alloc->end = pack_alloc->memchunk + size;
pack_alloc->endpos = pack_alloc->end;
pack_alloc->must_free = free;
allocator->alloc = ci_pack_allocator_alloc;
allocator->free = ci_pack_allocator_free;
allocator->reset = ci_pack_allocator_reset;
allocator->destroy = ci_pack_allocator_destroy;
allocator->data = pack_alloc;
allocator->name = NULL;
allocator->type = PACK_ALLOC;
allocator->must_free = free;
return allocator;
}
ci_mem_allocator_t *ci_create_pack_allocator(char *memblock, size_t size)
{
ci_mem_allocator_t *allocator;
pack_allocator_t *pack_alloc;
pack_alloc = ci_object_pool_alloc(PACK_ALLOCATOR_POOL);
if (!pack_alloc)
return NULL;
allocator = alloc_mem_allocator_struct();
if (!allocator) {
ci_object_pool_free(pack_alloc);
return NULL;
}
return init_pack_allocator(allocator, pack_alloc, memblock, size, 2);
}
/*similar to the above but allocates required space for pack_allocator on the given memblock*/
ci_mem_allocator_t *ci_create_pack_allocator_on_memblock(char *memblock, size_t size)
{
ci_mem_allocator_t *allocator;
/*We need to allocate space on memblock for internal structures*/
if (size <= (_CI_ALIGN(sizeof(pack_allocator_t)) + _CI_ALIGN(sizeof(ci_mem_allocator_t))))
return NULL;
pack_allocator_t *pack_alloc = (pack_allocator_t *)memblock;
memblock += _CI_ALIGN(sizeof(pack_allocator_t));
size -= _CI_ALIGN(sizeof(pack_allocator_t));
allocator = (ci_mem_allocator_t *)memblock;
memblock += _CI_ALIGN(sizeof(ci_mem_allocator_t));
size -= _CI_ALIGN(sizeof(ci_mem_allocator_t));
return init_pack_allocator(allocator, pack_alloc, memblock, size, 0);
}
int ci_pack_allocator_data_size(ci_mem_allocator_t *allocator)
{
assert(allocator->type == PACK_ALLOC);
pack_allocator_t *pack_alloc = (pack_allocator_t *)allocator->data;
return (int) (pack_alloc->curpos - pack_alloc->memchunk) +
(pack_alloc->end - pack_alloc->endpos);
}
size_t ci_pack_allocator_required_size()
{
return _CI_ALIGN(sizeof(pack_allocator_t)) + _CI_ALIGN(sizeof(ci_mem_allocator_t));
}
static size_t sizeof_pack_allocator() {return sizeof(pack_allocator_t);}
void ci_pack_allocator_set_start_pos(ci_mem_allocator_t *allocator, void *p)
{
pack_allocator_t *pack_alloc;
assert(allocator->type == PACK_ALLOC);
pack_alloc = (pack_allocator_t *)allocator->data;
assert((char *)p >= pack_alloc->memchunk);
pack_alloc->curpos = (char *)p;
}
void ci_pack_allocator_set_end_pos(ci_mem_allocator_t *allocator, void *p)
{
pack_allocator_t *pack_alloc;
assert(allocator->type == PACK_ALLOC);
pack_alloc = (pack_allocator_t *)allocator->data;
assert((char *)p <= pack_alloc->end);
if (p == NULL)
pack_alloc->endpos = pack_alloc->end;
else
pack_alloc->endpos = (char *)p;
}
/****************************************************************/
#define MEM_BLOCK_SIGNATURE 0xAAAA
#define FL_MEM_BLOCK_UNACCOUNTED 0x0001
struct mem_block_item {
uint16_t sig;
uint16_t flags;
struct mem_block_item *next;
union {
double __align;
char ptr[1];
} data;
};
#define MEM_BLOCK_DATA_OFFSET offsetof(struct mem_block_item, data.ptr[0])
struct pool_allocator {
char *name;
int items_size;
int strict;
int stat_allocs_id;
int stat_hits_id;
int stat_idle_id;
int stat_used_id;
int disable_stats;
ci_thread_mutex_t mutex;
struct mem_block_item *free;
};
static struct pool_allocator *pool_allocator_build(const char *name, int items_size, int strict)
{
char stat_group[256];
struct pool_allocator *palloc;
palloc = (struct pool_allocator *)malloc(sizeof(struct pool_allocator));
if (!palloc) {
return NULL;
}
palloc->name = name ? strdup(name) : NULL;
palloc->items_size = items_size;
palloc->strict = strict;
palloc->free = NULL;
palloc->disable_stats = 0;
snprintf(stat_group, sizeof(stat_group), "%s mem-pool", name);
ci_stat_group_register(stat_group, MEMPOOLS_STAT_MASTER_GROUP);
if ((palloc->stat_allocs_id = ci_stat_entry_register("Mallocs", CI_STAT_INT64_T, stat_group)) < 0)
palloc->disable_stats = 1;
if ((palloc->stat_hits_id = ci_stat_entry_register("Hits", CI_STAT_INT64_T, stat_group)) < 0)
palloc->disable_stats = 1;
if ((palloc->stat_idle_id = ci_stat_entry_register("Idle", CI_STAT_INT64_T, stat_group)) < 0)
palloc->disable_stats = 1;
if ((palloc->stat_used_id = ci_stat_entry_register("Used", CI_STAT_INT64_T, stat_group)) < 0)
palloc->disable_stats = 1;
if (palloc->disable_stats) {
ci_debug_printf(1,
"WARNING: Statistics for Memory pools \"%s\" are disabled\n"
"Are the statistics areas already built and initialized?\n",
name);
}
ci_thread_mutex_init(&palloc->mutex);
return palloc;
}
static void *pool_allocator_alloc(ci_mem_allocator_t *allocator,size_t size)
{
struct mem_block_item *mem_item;
struct pool_allocator *palloc = (struct pool_allocator *)allocator->data;
if (size > palloc->items_size)
return NULL;
ci_stat_memblock_t *STATS = palloc->disable_stats ? NULL : ci_stat_memblock_get();
ci_thread_mutex_lock(&palloc->mutex);
if (palloc->free) {
mem_item = palloc->free;
palloc->free=palloc->free->next;
if (STATS) {
STAT_INT64_INC_NL(STATS, palloc->stat_hits_id, 1);
if (!(mem_item->flags & FL_MEM_BLOCK_UNACCOUNTED))
STAT_INT64_DEC_NL(STATS, palloc->stat_idle_id, 1);
}
} else {
mem_item = malloc(palloc->items_size + MEM_BLOCK_DATA_OFFSET);
mem_item->sig = MEM_BLOCK_SIGNATURE;
mem_item->flags = 0;
mem_item->next = NULL;
if (STATS)
STAT_INT64_INC_NL(STATS, palloc->stat_allocs_id, 1);
}
if (STATS)
STAT_INT64_INC_NL(STATS, palloc->stat_used_id, 1);
else
mem_item->flags |= FL_MEM_BLOCK_UNACCOUNTED;
ci_thread_mutex_unlock(&palloc->mutex);
return (void *)mem_item->data.ptr;
}
static void pool_allocator_free(ci_mem_allocator_t *allocator,void *p)
{
struct mem_block_item *mem_item;
struct pool_allocator *palloc = (struct pool_allocator *)allocator->data;
ci_stat_memblock_t *STATS = palloc->disable_stats ? NULL : ci_stat_memblock_get();
ci_thread_mutex_lock(&palloc->mutex);
mem_item = (struct mem_block_item *)(p - MEM_BLOCK_DATA_OFFSET);
mem_item->next = palloc->free;
palloc->free = mem_item;
if (STATS) {
STAT_INT64_INC_NL(STATS, palloc->stat_idle_id, 1);
if (!(mem_item->flags & FL_MEM_BLOCK_UNACCOUNTED))
STAT_INT64_DEC_NL(STATS, palloc->stat_used_id, 1);
mem_item->flags = 0;
} else
mem_item->flags |= FL_MEM_BLOCK_UNACCOUNTED;
ci_thread_mutex_unlock(&palloc->mutex);
}
static void pool_allocator_reset(ci_mem_allocator_t *allocator)
{
struct mem_block_item *mem_item, *cur;
struct pool_allocator *palloc = (struct pool_allocator *)allocator->data;
ci_stat_memblock_t *STATS = palloc->disable_stats ? NULL : ci_stat_memblock_get();
ci_thread_mutex_lock(&palloc->mutex);
if (palloc->free) {
int freed = 0;
mem_item = palloc->free;
while (mem_item != NULL) {
cur = mem_item;
mem_item = mem_item->next;
free(cur);
freed++;
}
if (STATS)
STAT_INT64_DEC_NL(STATS, palloc->stat_idle_id, freed);
}
palloc->free = NULL;
ci_thread_mutex_unlock(&palloc->mutex);