-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
1686 lines (1272 loc) · 43.8 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
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <[email protected]>
*/
/*
=head1 NAME
I<libslack(mem)> - memory module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/mem.h>
typedef struct Pool Pool;
#define null NULL
#define nul '\0'
#define mem_new(type)
#define mem_create(size, type)
#define mem_resize(mem, size)
void *mem_resize_fn(void **mem, size_t size);
#define mem_release(mem)
void *mem_destroy(void **mem);
void *mem_create_secure(size_t size);
void mem_release_secure(void *mem);
void *mem_destroy_secure(void **mem);
char *mem_strdup(const char *str);
#define mem_create2d(type, x, y)
#define mem_create3d(type, x, y, z)
#define mem_create4d(type, x, y, z, a)
void *mem_create_space(size_t size, ...);
size_t mem_space_start(size_t size, ...);
#define mem_release2d(space)
#define mem_release3d(space)
#define mem_release4d(space)
#define mem_release_space(space)
#define mem_destroy2d(space)
#define mem_destroy3d(space)
#define mem_destroy4d(space)
#define mem_destroy_space(space)
Pool *pool_create(size_t size);
Pool *pool_create_with_locker(Locker *locker, size_t size);
void pool_release(Pool *pool);
void *pool_destroy(Pool **pool);
Pool *pool_create_secure(size_t size);
Pool *pool_create_secure_with_locker(Locker *locker, size_t size);
void pool_release_secure(Pool *pool);
void *pool_destroy_secure(Pool **pool);
void pool_clear_secure(Pool *pool);
#define pool_new(pool, type)
#define pool_newsz(pool, size, type)
void *pool_alloc(Pool *pool, size_t size);
void pool_clear(Pool *pool);
=head1 DESCRIPTION
This module is mostly just an interface to I<malloc(3)>, I<realloc(3)> and
I<free(3)> that tries to ensure that pointers that don't point to anything
get set to C<null>. It also provides dynamically allocated multi-dimensional
arrays, memory pools and secure memory for the more adventurous.
=over 4
=cut
*/
#include "config.h"
#include "std.h"
#ifdef HAVE_MLOCK
#include <sys/mman.h>
#endif
#include "err.h"
#include "mem.h"
struct Pool
{
size_t size; /* number of bytes in the pool */
size_t used; /* number of bytes allocated from the pool */
char *pool; /* address of the pool */
Locker *locker; /* locking strategy for the pool */
};
#ifndef TEST
/*
=item C< #define null NULL>
Easier to type. Easier to read. Feel free to keep using C<NULL> if you
prefer.
=item C< #define nul '\0'>
A name for the C<nul> character.
=item C< #define mem_new(type)>
Allocates enough memory (with I<malloc(3)>) to store an object of type
C<type>. It is the caller's responsibility to deallocate the allocated
memory with I<free(3)>, I<mem_release(3)>, or I<mem_destroy(3)>. It is
strongly recommended to use I<mem_destroy(3)>, because it also sets the
pointer variable to C<null>. On success, returns the address of the
allocated memory. On error, returns C<null>.
=item C< #define mem_create(size, type)>
Allocates enough memory (with I<malloc(3)>) to store C<size> objects of type
C<type>. It is the caller's responsibility to deallocate the allocated
memory with I<free(3)>, I<mem_release(3)>, or I<mem_destroy(3)>. It is
strongly recommended to use I<mem_destroy(3)>, because it also sets the
pointer variable to C<null>. On success, returns the address of the
allocated memory. On error, returns C<null>.
=item C< #define mem_resize(mem, num)>
Alters the amount of memory pointed to by C<*mem>. If C<*mem> is C<null>,
new memory is allocated and assigned to C<*mem>. If size is zero, C<*mem> is
deallocated and C<null> is assigned to C<*mem>. Otherwise, C<*mem> is
reallocated and assigned back to C<*mem>. On success, returns C<*mem> (which
will be C<null> if C<size> is zero). On error, returns C<null> with C<errno>
set appropriately, and C<*mem> is not altered.
=item C<void *mem_resize_fn(void **mem, size_t size)>
An interface to I<realloc(3)> that also assigns to a pointer variable unless
an error occurred. C<mem> points to the pointer to be affected. C<size> is
the requested size in bytes. If C<size> is zero, C<*mem> is deallocated and
set to C<null>. This function is exposed as an implementation side effect.
Don't call it directly. Call I<mem_resize(3)> instead. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
#ifdef HAVE_ISOC_REALLOC
#define isoc_realloc realloc
#else
static void *isoc_realloc(void *ptr, size_t size)
{
void *p;
if (size)
{
if (!(p = (ptr) ? realloc(ptr, size) : malloc(size)))
errno = ENOMEM; /* Not required by ISO C but handy */
}
else
{
free(ptr);
p = NULL;
}
return p;
}
#endif
void *mem_resize_fn(void **mem, size_t size)
{
void *ptr;
if (!mem)
return set_errnull(EINVAL);
ptr = isoc_realloc(*mem, size);
if (size && !ptr)
return NULL;
return *mem = ptr;
}
/*
=item C< #define mem_release(mem)>
Releases (deallocates) C<mem>. Same as I<free(3)>. Only to be used in
destructor functions. In other cases, use I<mem_destroy(3)> which also sets
C<mem> to C<null>.
=item C<void *mem_destroy(void **mem)>
Calls I<free(3)> on the pointer, C<*mem>. Then assigns C<null> to this
pointer. Returns C<null>.
=cut
*/
void *(mem_destroy)(void **mem)
{
if (mem && *mem)
{
free(*mem);
*mem = NULL;
}
return NULL;
}
/*
=item C<void *mem_create_secure(size_t size)>
Allocates C<size> bytes of memory (with I<malloc(3)>), and then locks it
into RAM with I<mlock(2)> so that it can't be paged to disk, where some
nefarious local user with root access might read its contents. Note that
additional operating system dependent measures might be required to prevent
the I<root> user from accessing the RAM of arbitrary processes (e.g. On
I<Linux>: C<sysctl kernel.yama.ptrace_scope=3>). It is the caller's
responsibility to deallocate the secure memory with I<mem_release_secure(3)>
or I<mem_destroy_secure(3)> which will clear the memory and unlock it before
deallocating it. It is strongly recommended to use I<mem_destroy_secure(3)>,
because it also sets the pointer variable to C<null>. On success, returns
the address of the secure allocated memory. On error, returns C<null> with
C<errno> set appropriately.
Note that entire memory pages are locked by I<mlock(2)>, so don't create
many, small pieces of secure memory or many entire pages will be locked. Use
a secure memory pool instead. Also note that on old systems, secure memory
requires root privileges.
On some systems (e.g. I<Solaris>), memory locks must start on page
boundaries. So we need to I<malloc(3)> enough memory to extend from whatever
address I<malloc(3)> may return to the next page boundary (worst case:
C<pagesize - sizeof(int)>) and then the actual number of bytes requested. We
need an additional C<sizeof(void *) + sizeof(size_t)> bytes (e.g. C<8> or
C<16>) to store the address returned by I<malloc(3)> (so we can I<free(3)>
it later), and the size passed to I<mlock(2)> so we can pass it to
I<munlock(2)> later. Unfortunately, we need to store the address and the
size after the page boundary and not before it, because I<malloc(3)> might
return a page boundary or an address less than C<sizeof(void *) +
sizeof(size_t)> bytes to the left of a page boundary.
It will look like:
for free()
+-------+ +- size+n for munlock()
v | v
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |* * * *|# # # #| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
^ ^ ^ . . . size bytes . . . . . .
| +- next page |
+- malloc() +- address returned
If your system doesn't require page boundaries (e.g. I<Linux>), the address
returned by I<malloc(3)> is locked, and only the size is stored.
=cut
*/
void *mem_create_secure(size_t size)
{
#ifdef HAVE_MLOCK
char *addr, *lock;
#ifdef MLOCK_REQUIRES_PAGE_BOUNDARY
long pagesize;
if ((pagesize = sysconf(_SC_PAGESIZE)) == -1)
return set_errnull(EINVAL);
size += sizeof(void *) + sizeof(size_t);
addr = malloc(pagesize - sizeof(int) + size);
#else
size += sizeof(size_t);
addr = malloc(size);
#endif
if (!addr)
return NULL;
#ifdef MLOCK_REQUIRES_PAGE_BOUNDARY
if ((long)addr & (pagesize - 1)) /* addr not on page boundary */
lock = (void *)(((long)addr & ~(pagesize - 1)) + pagesize);
else
lock = addr;
#else
lock = addr;
#endif
if (mlock(lock, size) == -1)
{
free(addr);
return NULL;
}
#ifdef MLOCK_REQUIRES_PAGE_BOUNDARY
*(void **)lock = addr;
lock += sizeof(void *);
#endif
*(size_t *)lock = size;
lock += sizeof(size_t);
return lock;
#else
errno = ENOSYS;
return NULL;
#endif
}
/*
=item C<void mem_release_secure(void *mem)>
Sets the memory pointed to by C<mem> to C<0xff> bytes, then to C<0xaa>
bytes, then to C<0x55> bytes, then to C<nul> bytes, then unlocks and
releases (deallocates) C<mem>. Only to be used on memory returned by
I<mem_create_secure(3)>. Only to be used in destructor functions. In other
cases, use I<mem_destroy_secure(3)> which also sets C<mem> to C<null>.
=cut
*/
void mem_release_secure(void *mem)
{
#ifdef HAVE_MLOCK
char *addr, *lock;
size_t size;
if (!mem)
return;
lock = mem;
lock -= sizeof(size_t);
size = *(size_t *)lock;
#ifdef MLOCK_REQUIRES_PAGE_BOUNDARY
lock -= sizeof(void *);
addr = *(void **)lock;
#else
addr = lock;
#endif
memset(lock, 0xff, size);
memset(lock, 0xaa, size);
memset(lock, 0x55, size);
memset(lock, 0x00, size);
munlock(lock, size);
free(addr);
#endif
}
/*
=item C<void *mem_destroy_secure(void **mem)>
Sets the memory pointed to by C<*mem> to C<0xff> bytes, then to C<0xaa>
bytes, then to C<0x55> bytes, then to C<nul> bytes, then unlocks and
destroys (deallocates and sets to C<null>) C<*mem>. Only to be used on
memory returned by I<mem_create_secure(3)>. Returns C<null>.
=cut
*/
void *(mem_destroy_secure)(void **mem)
{
if (mem && *mem)
{
mem_release_secure(*mem);
*mem = NULL;
}
return NULL;
}
/*
=item C<char *mem_strdup(const char *str)>
Returns a dynamically allocated copy of C<str>. It is the caller's
responsibility to deallocate the new string with I<free(3)>,
I<mem_release(3)>, or I<mem_destroy(3)>. It is strongly recommended to use
I<mem_destroy(3)>, because it also sets the pointer variable to C<null>.
This function exists because I<strdup(3)> is not part of the I<ISO C>
standard. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
char *mem_strdup(const char *str)
{
size_t size;
char *copy;
if (!str)
return set_errnull(EINVAL);
if (!(copy = mem_create(size = strlen(str) + 1, char)))
return NULL;
return memcpy(copy, str, size);
}
/*
=item C< #define mem_create2d(i, j, type)>
Alias for allocating a 2-dimensional array. See I<mem_create_space(3)>.
=item C< #define mem_create3d(i, j, k, type)>
Alias for allocating a 3-dimensional array. See I<mem_create_space(3)>.
=item C< #define mem_create4d(i, j, k, l, type)>
Alias for allocating a 4-dimensional array. See I<mem_create_space(3)>.
=item C<void *mem_create_space(size_t size, ...)>
Allocates a multi-dimensional array of elements of size C<size> and sets the
memory to C<nul> bytes. The remaining arguments specify the sizes of each
dimension. The last argument must be zero. There is an arbitrary limit of 32
dimensions. The memory returned is set to C<nul> bytes. The memory returned
needs to be cast or assigned into the appropriate pointer type. You can then
set and access elements exactly like a real multi-dimensional C array.
Finally, it must be deallocated with I<mem_destroy_space(3)> or
I<mem_release_space(3)> or I<mem_destroy(3)> or I<mem_release(3)> or
I<free(3)>. It is strongly recommended to use I<mem_destroy_space(3)> or
I<mem_destroy(3)>, because they also set the pointer variable to C<null>.
Note: You must not use I<memset(3)> on all of the returned memory because
the start of this memory contains pointers into the remainder. The exact
amount of this overhead depends on the number and size of dimensions. The
memory is allocated with I<calloc(3)> to reduce the need to I<memset(3)> the
elements, but if you need to know where the elements begin, use
I<mem_space_start(3)>.
The memory returned looks like (e.g.):
char ***a = mem_create3d(2, 2, 3, char);
+-------------------------+
+-------|-------------------+ |
a +-------|-------|-------------+ | |
| +-------|-------|-------|-------+ | | |
v | | | | V V V V
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| a[0] | a[1] |a[0][0]|a[0][1]|a[1][0]|a[1][1]| | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | ^ ^ a a a a a a a a a a a a
+-------|-------+ | 0 0 0 0 0 0 1 1 1 1 1 1
+-----------------------+ 0 0 0 1 1 1 0 0 0 1 1 1
0 1 2 0 1 2 0 1 2 0 1 2
=cut
*/
#ifndef MEM_MAX_DIM
#define MEM_MAX_DIM 32
#endif
void *mem_create_space(size_t size, ...)
{
size_t dim[MEM_MAX_DIM], d, i, j;
size_t lengths[MEM_MAX_DIM];
size_t starts[MEM_MAX_DIM];
size_t sizes[MEM_MAX_DIM];
char *space;
size_t arg, length;
va_list args;
va_start(args, size);
for (d = 0; d < MEM_MAX_DIM && (arg = va_arg(args, size_t)); ++d)
dim[d] = arg;
va_end(args);
for (length = i = 0; i < d; ++i)
{
starts[i] = length;
lengths[i] = sizes[i] = (i == d - 1) ? size : sizeof(void *);
for (j = 0; j <= i; ++j)
lengths[i] *= dim[j];
length += lengths[i];
}
if (!(space = calloc(length, 1)))
return NULL;
for (i = 0; i < d - 1; ++i)
{
size_t num = dim[i];
for (j = 0; j < i; ++j)
num *= dim[j];
for (j = 0; j < num; ++j)
*(char **)(space + starts[i] + j * sizes[i]) = space + starts[i + 1] + j * dim[i + 1] * sizes[i + 1];
}
return space;
}
/*
=item C<size_t mem_space_start(size_t size, ...)>
Calculates the amount of overhead required for a dynamically allocated
multi-dimensional array created by a call to I<mem_create_space(3)> with the
same arguments. If you need to reset all elements in such an array to C<nul>
bytes:
int ****space = mem_create_space(sizeof(int), 2, 3, 4, 5, 0);
size_t start = mem_space_start(sizeof(int), 2, 3, 4, 5, 0);
memset((char *)space + start, '\0', sizeof(int) * 2 * 3 * 4 * 5);
=cut
*/
size_t mem_space_start(size_t size, ...)
{
size_t dim[MEM_MAX_DIM], d, i, j;
size_t lengths[MEM_MAX_DIM];
size_t arg, length;
va_list args;
va_start(args, size);
for (d = 0; d < MEM_MAX_DIM && (arg = va_arg(args, size_t)); ++d)
dim[d] = arg;
va_end(args);
for (length = i = 0; i < d; ++i)
{
lengths[i] = (i == d - 1) ? size : sizeof(void *);
for (j = 0; j <= i; ++j)
lengths[i] *= dim[j];
length += lengths[i];
}
return length - lengths[d - 1];
}
/*
=item C< #define mem_release2d(space)>
Alias for releasing (deallocating) a dynamically allocated 2-dimensional
array. See I<mem_release_space(3)>.
=item C< #define mem_release3d(space)>
Alias for releasing (deallocating) a dynamically allocated 3-dimensional
array. See I<mem_release_space(3)>.
=item C< #define mem_release4d(space)>
Alias for releasing (deallocating) a dynamically allocated 4-dimensional
array. See I<mem_release_space(3)>.
=item C< #define mem_release_space(space)>
Releases (deallocates) a multi-dimensional array, C<space>, allocated with
I<mem_create_space>. Same as I<free(3)>. Only to be used in destructor
functions. In other cases, use I<mem_destroy_space(3)> or I<mem_destroy>
which also set C<space> to C<null>.
=item C< #define mem_destroy2d(space)>
Alias for destroying (deallocating and setting to C<null>) a
2-dimensional array. See I<mem_destroy_space(3)>.
=item C< #define mem_destroy3d(space)>
Alias for destroying (deallocating and setting to C<null>) a
3-dimensional array. See I<mem_destroy_space(3)>.
=item C< #define mem_destroy4d(space)>
Alias for destroying (deallocating and setting to C<null>) a
4-dimensional array. See I<mem_destroy_space(3)>.
=item C< #define mem_destroy_space(mem)>
Destroys (deallocates and sets to C<null>) the multi-dimensional array
pointed to by C<space>.
=cut
*/
/*
=item C<Pool *pool_create(size_t size)>
Creates a memory pool of size C<size> from which smaller chunks of memory
may be subsequently allocated (with I<pool_alloc(3)>) without resorting to
the use of I<malloc(3)>. Useful when you have many small objects to
allocate, but I<malloc(3)> is slowing your program down too much. It is the
caller's responsibility to deallocate the new pool with I<pool_release(3)>
or I<pool_destroy(3)>. It is strongly recommended to use I<pool_destroy(3)>,
because it also sets the pointer variable to C<null>. On success, returns
the pool. On error, returns C<null>.
The size of a pool can't be changed after it is created, and the individual
chunks of memory allocated from within a pool can't be separately
deallocated. The entire pool can be emptied with I<pool_clear(3)>.
=cut
*/
Pool *pool_create(size_t size)
{
return pool_create_with_locker(NULL, size);
}
/*
=item C<Pool *pool_create_with_locker(Locker *locker, size_t size)>
Equivalent to I<pool_create(3)> except that multiple threads accessing the
new pool will be synchronised by C<locker>.
=cut
*/
Pool *pool_create_with_locker(Locker *locker, size_t size)
{
Pool *pool = mem_create(1, Pool);
if (!pool)
return NULL;
if (!(pool->pool = malloc(size)))
{
mem_release(pool);
return NULL;
}
pool->size = size;
pool->used = 0;
pool->locker = locker;
return pool;
}
/*
C<int pool_lock(Pool *pool)>
Claims a write lock on C<pool>. On success, returns C<0>. On error, returns
an error code.
C<int pool_unlock(Pool *pool)>
Unlocks a write lock on C<pool>. On success, returns C<0>. On error, returns
an error code.
*/
#define pool_lock(pool) locker_wrlock((pool)->locker)
#define pool_unlock(pool) locker_unlock((pool)->locker)
/*
=item C<void pool_release(Pool *pool)>
Releases (deallocates) C<pool>. Only to be used in destructor functions. In
other cases, use I<pool_destroy(3)> which also sets C<pool> to C<null>.
=cut
*/
void pool_release(Pool *pool)
{
Locker *locker;
int err;
if (!pool)
return;
if ((err = pool_lock(pool)))
{
set_errno(err);
return;
}
locker = pool->locker;
mem_release(pool->pool);
mem_release(pool);
if ((err = locker_unlock(locker)))
set_errno(err);
}
/*
=item C<void *pool_destroy(Pool **pool)>
Destroys (deallocates and sets to C<null>) C<*pool>. Returns C<null>.
B<Note:> pools shared by multiple threads must not be destroyed until after
all threads have finished with it.
=cut
*/
void *pool_destroy(Pool **pool)
{
if (pool && *pool)
{
pool_release(*pool);
*pool = NULL;
}
return NULL;
}
/*
=item C<Pool *pool_create_secure(size_t size)>
Creates a memory pool of size C<size> just like I<pool_create(3)> except
that the memory pool is locked into RAM with I<mlock(2)> so that it can't be
paged to disk where some nefarious local user with root access might read
its contents. Note that additional operating system dependent measures might
be required to prevent the I<root> user from accessing the RAM of arbitrary
processes (e.g. On I<Linux>: C<sysctl kernel.yama.ptrace_scope=3>). It is
the caller's responsibility to deallocate the new pool with
I<pool_release_secure(3)> or I<pool_destroy_secure(3)> which will clear the
memory pool and unlock it before deallocating it. In all other ways, the
pool returned is exactly like a pool returned by I<pool_create(3)>. On
success, returns the pool. On error, returns C<null> with C<errno> set
appropriately. Note that on old systems, secure memory requires root
privileges.
=cut
*/
Pool *pool_create_secure(size_t size)
{
return pool_create_secure_with_locker(NULL, size);
}
/*
=item C<Pool *pool_create_secure_with_locker(Locker *locker, size_t size)>
Equivalent to I<pool_create_secure(3)> except that multiple threads accessing
the new pool will be synchronised by C<locker>.
=cut
*/
Pool *pool_create_secure_with_locker(Locker *locker, size_t size)
{
#ifdef HAVE_MLOCK
Pool *pool = mem_create(1, Pool);
if (!pool)
return NULL;
if (!(pool->pool = mem_create_secure(size)))
{
mem_release(pool);
return NULL;
}
pool->size = size;
pool->used = 0;
pool->locker = locker;
return pool;
#else
errno = ENOSYS;
return NULL;
#endif
}
/*
=item C<void pool_release_secure(Pool *pool)>
Sets the contents of the memory pool to C<0xff> bytes, then to C<0xaa>
bytes, then to C<0x55> bytes, then to C<nul> bytes, then unlocks and
releases (deallocates) C<pool>. Only to be used on pools returned by
I<pool_create_secure(3)>. Only to be used in destructor functions. In other
cases, use I<pool_destroy_secure(3)> which also sets C<pool> to C<null>.
=cut
*/
void pool_release_secure(Pool *pool)
{
#ifdef HAVE_MLOCK
Locker *locker;
int err;
if (!pool)
return;
if ((err = pool_lock(pool)))
{
set_errno(err);
return;
}
locker = pool->locker;
mem_release_secure(pool->pool);
mem_release(pool);
if ((err = locker_unlock(locker)))
set_errno(err);
#endif
}
/*
=item C<void *pool_destroy_secure(Pool **pool)>
Sets the contents of the memory pool to C<0xff> bytes, then to C<0xaa>
bytes, then to C<0x55> bytes, then to C<nul> bytes, then unlocks and
destroys (deallocates and sets to C<null>) C<*pool>. Returns C<null>.
B<Note:> secure pools shared by multiple threads must not be destroyed until
after all threads have finished with it.
=cut
*/
void *pool_destroy_secure(Pool **pool)
{
if (pool && *pool)
{
pool_release_secure(*pool);
*pool = NULL;
}
return NULL;
}
/*
=item C<void pool_clear_secure(Pool *pool)>
Fills the secure C<pool> with C<0xff> bytes, then C<0xaa> bytes, then
C<0x55> bytes, then C<nul> bytes, and deallocates all of the chunks of
secure memory previously allocated from C<pool> so that it can be reused.
Does not use I<free(3)>.
=cut
*/
static void pool_clear_unlocked(Pool *pool);
void pool_clear_secure(Pool *pool)
{
int err;
if (!pool)
return;
if ((err = pool_lock(pool)))
{
set_errno(err);
return;
}
pool_clear_unlocked(pool);
memset(pool->pool, 0xff, pool->size);
memset(pool->pool, 0xaa, pool->size);
memset(pool->pool, 0x55, pool->size);
memset(pool->pool, 0x00, pool->size);
if ((err = pool_unlock(pool)))
set_errno(err);
}
/*
=item C< #define pool_new(pool, type)>
Allocates enough memory from C<pool> to store an object of type C<type>. On
success, returns the address of the allocated memory. On error, returns
C<null> with C<errno> set appropriately.
=item C< #define pool_newsz(pool, size, type)>
Allocates enough memory from C<pool> to store C<size> objects of type
C<type>. On success, returns the address of the allocated memory. On error,
returns C<null> with C<errno> set appropriately.
=item C<void *pool_alloc(Pool *pool, size_t size)>
Allocates a chunk of memory of C<size> bytes from C<pool>. Does not use
I<malloc(3)>. The pointer returned must not be passed to I<free(3)> or
I<realloc(3)>. Only the entire pool can be deallocated with
I<pool_release(3)> or I<pool_destroy(3)>. All of the chunks can be
deallocated in one go with I<pool_clear(3)> without deallocating the pool
itself.
On success, returns the pointer to the allocated pool memory. On error,
returns C<null> with C<errno> set appropriately (i.e. C<EINVAL> if C<pool>
is C<null>, C<ENOSPC> if C<pool> does not have enough unused memory to
allocate C<size> bytes).
It is the caller's responsibility to ensure the correct alignment if
necessary by allocating the right numbers of bytes. The easiest way to do
ensure is to use separate pools for each specific data type that requires
specific alignment.
=cut
*/
void *pool_alloc(Pool *pool, size_t size)
{
void *addr;
int err;
if (!pool)
return set_errnull(EINVAL);
if ((err = pool_lock(pool)))
return set_errnull(err);
if (pool->used + size > pool->size)
{
pool_unlock(pool);
return set_errnull(ENOSPC);
}
addr = pool->pool + pool->used;
pool->used += size;
if ((err = pool_unlock(pool)))
return set_errnull(err);
return addr;
}
/*
=item C<void pool_clear(Pool *pool)>
Deallocates all of the chunks of memory previously allocated from C<pool> so
that it can be reused. Does not use I<free(3)>.
=cut
*/
static void pool_clear_with_locker(Pool *pool, int lock_pool)
{
int err;
if (!pool)
return;
if (lock_pool && (err = pool_lock(pool)))
{
set_errno(err);
return;
}
pool->used = 0;
if (lock_pool && (err = pool_unlock(pool)))
set_errno(err);
}
static void pool_clear_unlocked(Pool *pool)