-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp.c
1394 lines (1271 loc) · 34.3 KB
/
mp.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
/****************************************************************************
|*
|* tap3edit Tools (http://www.tap3edit.com)
|*
|* Copyright (c) 2014-2018, Javier Gutierrez <https://github.com/tap3edit/mp>
|*
|* Permission to use, copy, modify, and/or distribute this software for any
|* purpose with or without fee is hereby granted, provided that the above
|* copyright notice and this permission notice appear in all copies.
|*
|* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|*
|*
|* Module: mp.c
|*
|* Description: Memory pool management
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|*
|* When Who Pos What
|* 20140801 JG Initial version
|*
****************************************************************************/
/* Includes and defines */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _WIN32 /* { */
# include <STDDEF.h>
#else /* } _WIN32 { */
# include <stdint.h>
#endif /* } _WIN32 */
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <valgrind/valgrind.h>
#ifndef MP_VALGRIND_NOT_AVAILABLE /* { */
# pragma message("Compiled with the Valgrind extension")
# include <valgrind/memcheck.h>
#endif /* } MP_VALGRIND_NOT_AVAILABLE */
#include "mp.h"
/* Prototypes */
static void *mpget_chunk(size_t size, int mpid, size_t alignment);
static void *mpadd_block(size_t size, int mpid, size_t alignment);
static int mpadd_tot_phy_mem(size_t size2add, int sign);
/* Local variables */
#if MP_THREAD_SAFE == 1 /* { */
static MP_TLS_INT mp_cur_mpid = MP_DEF_MP_ID; /* Current Memory Pool ID */
static MP_TLS_INT mp_prev_mpid = MP_NO_MP_ID; /* Previous Memory Pool ID */
# ifndef _WIN32
static MP_MUTEX_T mp_mutex = MP_MUTEX_INIT_VAL; /* Mutex */
static MP_MUTEX_T *mp_mutex_p = &mp_mutex; /* Mutex Pointer */
# else
static MP_MUTEX_T *mp_mutex_p = NULL; /* Mutex Pointer */
# endif
#else /* } MP_THREAD_SAFE { */
static int mp_cur_mpid = MP_DEF_MP_ID; /* Current Memory Pool ID */
static int mp_prev_mpid = MP_NO_MP_ID; /* Previous Memory Pool ID */
#endif /* } */
static size_t mp_tot_phy_mem = 0; /* Total physical memory used */
static size_t volatile mp_mem_limit = 0; /* Memory limit */
static size_t volatile mp_blk_sz = MP_DEF_BLK_SZ; /* Memory limit */
/* Global variables */
#if MP_THREAD_SAFE == 1
MP_TLS_INT mperrno = 0;
MP_TLS_CHAR *mperrstr = NULL;
#else
int mperrno = 0;
char *mperrstr = NULL;
#endif
mp mp_arr[MP_MAX_MP_ID -1]; /* Array of memory pools */
/****************************************************************************
|*
|* Function: mpadd_block
|*
|* Description;
|*
|* Adds a new block to our memory pool.
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
static void *mpadd_block(size_t size, int mpid, size_t alignment)
{
void *chunk = NULL;
size_t block_size = mpget_blksz() + alignment -1;
mpblock *curr_block = NULL;
mpblock *new_block = NULL;
mperrno = MP_ERRNO_SUCCESS;
/* Parameters check was done on mpget_chunk() */
/* Check if the memory pool was initialized */
if (mp_arr[mpid].init != 'Y')
{
if (mpid == MP_DEF_MP_ID)
{
mp_arr[mpid].init = 'Y';
strncpy(mp_arr[mpid].descr, MP_DEF_MP_DESCR, sizeof(mp_arr[mpid].descr));
mp_arr[mpid].descr[sizeof(mp_arr[mpid].descr) -1] = '\0';
#if MP_THREAD_SAFE == 1
/* Threads should aquire a new Memory Pool, so we assume
* The thread calling the default one is the main thread */
mp_arr[mpid].thread_id = MP_CURR_THREAD;
#endif
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_CREATE_MEMPOOL((void *)&mp_arr[mpid], 0, 1);
#endif
}
else
{
mperrno = MP_ERRNO_NOIN;
return NULL;
}
}
/* Set memory limit */
if (mp_mem_limit <= 0) /* Don't care about thread race */
{
if (sizeof(size_t) > 4)
{
mp_mem_limit = MP_DEF_MEM_LIMIT_64;
}
else
{
mp_mem_limit = MP_DEF_MEM_LIMIT_32;
}
}
/* Creating new memory pool block */
new_block = (mpblock *)malloc(sizeof(mpblock));
if (new_block == NULL)
{
mperrno = MP_ERRNO_ALLO;
return NULL;
}
memset(new_block, 0x00, sizeof(mpblock));
new_block->block = NULL;
new_block->next = NULL;
/* Override default size if needed */
if (size > mpget_blksz())
{
block_size = size + alignment -1;
}
/* Check memory limit */
if ((mperrno = mpadd_tot_phy_mem(block_size, +1)) != MP_ERRNO_SUCCESS)
{
free(new_block);
return NULL;
}
/* Setting new memory pool block info */
new_block->block = (uchar *)malloc(block_size);
if (new_block->block == NULL)
{
mperrno = MP_ERRNO_ALLO;
return NULL;
}
new_block->size = block_size;
new_block->used = alignment - ((uintptr_t)new_block->block % alignment);
new_block->used = new_block->used == alignment ? 0 : new_block->used;
chunk = new_block->block + new_block->used;
new_block->used += size;
new_block->next = NULL;
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_MAKE_MEM_NOACCESS(new_block->block, new_block->size);
#endif
/* Attach new memory block to our memory pool */
if (mp_arr[mpid].head_block != NULL)
{
curr_block = mp_arr[mpid].tail_block;
curr_block->next = new_block;
}
else
{
mp_arr[mpid].head_block = new_block;
}
mp_arr[mpid].tail_block = new_block;
/* Deliver required chunk of memory */
return chunk;
}
/****************************************************************************
|*
|* Function: mpget_chunk
|*
|* Description;
|*
|* Gets a chunk of memory of the specified size from the memmory pool
|* with id mpid and aligned to alignment.
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
static void *mpget_chunk(size_t size, int mpid, size_t alignment)
{
void *chunk = NULL;
mp *curr_mp = NULL;
mpblock *curr_block = NULL;
mperrno = MP_ERRNO_SUCCESS;
/* We decided not to support negative sizes */
if (size < 0) /* Works only when size_t is signed */
{
mperrno = MP_ERRNO_SZNG;
return NULL;
}
/* Memory pool ID out of limit */
if (mpid > MP_MAX_MP_ID -1 || mpid < 0)
{
mperrno = MP_ERRNO_MPID;
return NULL;
}
/* Memory pool ID not initiliazed */
if (mp_arr[mpid].init != 'Y' && mpid != MP_DEF_MP_ID)
{
mperrno = MP_ERRNO_NOIN;
return NULL;
}
/* Alignment out of limits: Alignment is not power of 2 or is less than size of void * */
if ((alignment & (alignment -1)) != 0 ||
alignment < sizeof(void *))
{
mperrno = MP_ERRNO_EXAL;
return NULL;
}
/* Setting right alignment */
if (alignment < MP_DEF_ALIGN)
{
alignment = MP_DEF_ALIGN;
}
/* Not to fail if size is 0 we deliver a pointer anyway */
if (size == 0)
{
/* TODO: In this case we should return some lost memory chunk */
size = 1;
}
curr_mp = &mp_arr[mpid];
#if MP_THREAD_SAFE == 1
/* Check thread ID */
if (!(mpid == MP_DEF_MP_ID && curr_mp->init != 'Y'))
{
if (MP_THREAD_EQ(curr_mp->thread_id, MP_CURR_THREAD) == 0)
{
mperrno = MP_ERRNO_THRD;
return NULL;
}
}
#endif
/* Get memory chunk */
if (curr_mp->head_block == NULL)
{
/* First time using this pool, creating block with right alignment */
chunk = mpadd_block(size, mpid, alignment);
}
else
{
/* Block already in used */
int margin = 0;
/* Check if last memory block has enough space */
curr_block = curr_mp->tail_block;
margin = alignment - ((uintptr_t)(curr_block->block + curr_block->used) % alignment);
margin = margin == alignment ? 0 : margin;
if (curr_block->size > curr_block->used + margin + size)
{
curr_block->used += margin;
chunk = curr_block->block + curr_block->used;
curr_block->used += size;
}
if (chunk == NULL)
{
/* No space in last memory block, creating new memory block */
chunk = mpadd_block(size, mpid, alignment);
}
}
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_MEMPOOL_ALLOC((void *)&mp_arr[mpid], chunk, size);
#endif
return chunk;
}
/****************************************************************************
|*
|* Function: mpmalloc
|*
|* Description;
|*
|* Analog to malloc() but allocating a piece of memory from our private
|* pool on current pool id (selected with mpset()).
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mpmalloc(size_t size)
{
return mpmalloc_mpid(size, mp_cur_mpid);
}
/****************************************************************************
|*
|* Function: mpmalloc_mpid
|*
|* Description;
|*
|* Analog to malloc() but allocating a piece of memory from our private
|* pool on a certain pool id
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mpmalloc_mpid(size_t size, int mpid)
{
return mpget_chunk(size, mpid, MP_DEF_ALIGN);
}
/****************************************************************************
|*
|* Function: mpmemalign
|*
|* Description;
|*
|* Analog to memalign() but allocating a piece of memory from our private
|* pool on current pool id (selected with mpset()) and aligned to the
|* desired alignment.
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mpmemalign(size_t alignment, size_t size)
{
return mpmemalign_mpid(alignment, size, mp_cur_mpid);
}
/****************************************************************************
|*
|* Function: mpmemalign_mpid
|*
|* Description;
|*
|* Analog to malloc() but allocating a piece of memory from our private
|* pool on a certain pool id and aligned to the desired alignment.
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mpmemalign_mpid(size_t alignment, size_t size, int mpid)
{
return mpget_chunk(size, mpid, alignment);
}
/****************************************************************************
|*
|* Function: mpcalloc
|*
|* Description;
|*
|* Analog to calloc() but allocating a piece of memory from our private
|* pool on current pool id (selected with mpset()).
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mpcalloc(size_t nelem, size_t size)
{
return mpcalloc_mpid(nelem, size, mp_cur_mpid);
}
/****************************************************************************
|*
|* Function: mpcalloc_mpid
|*
|* Description;
|*
|* Analog to calloc() but allocating a piece of memory from our private
|* pool on a certain pool id
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mpcalloc_mpid(size_t nelem, size_t size, int mpid)
{
/* Allocate */
void *chunk = mpget_chunk(size * nelem, mpid, MP_DEF_ALIGN);
/* Initialize */
if (chunk != NULL)
{
memset(chunk, 0x00, size * nelem);
}
return chunk;
}
/****************************************************************************
|*
|* Function: mpfree
|*
|* Description;
|*
|* Analog to free() but it's a dummy function that doesn't do anything.
|* The reason is that this memory pool is a dynamic memory pool allocated
|* for which the single memory chunks cannot be free-ed, but only the
|* whole memory pool. So the reason of this funcion is for 3rd party software
|* enabling a custom memory management and on which the malloc(), realloc(),
|* free(), etc. can be overriden with own functions.
|*
|* Return:
|* n/a
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void mpfree(void *ptr)
{
mpfree_mpid(ptr, mp_cur_mpid);
}
/****************************************************************************
|*
|* Function: mpfree_mpid
|*
|* Description;
|*
|* Analog to free() but it's a dummy function that doesn't do anything.
|* The reason is that this memory pool is a dynamic memory pool allocated
|* for which the single memory chunks cannot be free-ed, but only the
|* whole memory pool. So the reason of this funcion is for 3rd party software
|* enabling a custom memory management and on which the malloc(), realloc(),
|* free(), etc. can be overriden with own functions.
|*
|* Return:
|* n/a
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void mpfree_mpid(void *ptr, int mpid)
{
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_MEMPOOL_FREE((void *)&mp_arr[mpid], ptr);
#endif
return;
}
/****************************************************************************
|*
|* Function: mprealloc
|*
|* Description;
|*
|* Analog to realloc() but allocating a piece of memory from our private
|* pool on current pool id (selected with mpset()).
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool and with the content of ptr if available.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mprealloc(void *ptr, size_t size)
{
return mprealloc_mpid(ptr, size, mp_cur_mpid);
}
/****************************************************************************
|*
|* Function: mprealloc_mpid
|*
|* Description;
|*
|* Analog to realloc() but allocating a piece of memory from our private
|* pool on a certain pool id
|*
|* Return:
|* a pointer to a block of memory of the required size within our memory
|* pool and with the content of ptr if available.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
void *mprealloc_mpid(void *ptr, size_t size, int mpid)
{
void *chunk = NULL;
uintptr_t ptrdiff = 0;
/* Allocate new chunk of memory */
chunk = mpget_chunk(size, mpid, MP_DEF_ALIGN);
/* Copy content of all memory chunk */
if (ptr != NULL && chunk != NULL)
{
/* Valgrind can yell here invalid or initiliazed read.
* At this point we don't know the size of previous pointer, so
* we need to copy as much as possible, therefore part of it
* might not be initiliazed */
/* Not to overlap */
ptrdiff = (uintptr_t)chunk - (uintptr_t)ptr;
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_DISABLE_ERROR_REPORTING;
#endif
if (ptrdiff < size)
{
memcpy(chunk, ptr, ptrdiff);
}
else
{
memcpy(chunk, ptr, size);
}
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_ENABLE_ERROR_REPORTING;
#endif
}
return chunk;
}
/****************************************************************************
|*
|* Function: mpstrdup
|*
|* Description;
|*
|* Returns a pointer to a new string that is a duplicate of s1.
|*
|* Return:
|* a pointer to duplicated string.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
char *mpstrdup(const char *s1)
{
return mpstrdup_mpid(s1, mp_cur_mpid);
}
/****************************************************************************
|*
|* Function: mpstrdup_mpid
|*
|* Description;
|*
|* Returns a pointer to a new string that is a duplicate of s1.
|*
|* Return:
|* a pointer to duplicated string or NULL if s1 is NULL.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
char *mpstrdup_mpid(const char *s1, int mpid)
{
char *str = NULL;
size_t str_len = 0;
/* Check parameters */
if (s1 == NULL)
{
return NULL;
}
str_len = strlen(s1) +1;
/* Allocate memory for new string */
str = (char *)mpmalloc_mpid(str_len, mpid);
/* Duplicate string in allocated memory */
if (str != NULL)
{
strncpy(str, s1, str_len);
}
return str;
}
/****************************************************************************
|*
|* Function: mpasprintf_mpid
|*
|* Description;
|*
|* anolog to sprintf() except that allocates a string large enough to hold
|* the output. Same functionality of asprintf().
|*
|* Return:
|* The number of bytes allocated on success
|* -1 on error and the content of strp is undefined on error.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140815 JG Initial version
|*
****************************************************************************/
int mpasprintf(char **strp, const char *fmt, ...)
{
int rc = 0;
int len = 0;
va_list ap;
/* Check parameters */
if (fmt == NULL || strp == NULL)
{
mperrno = MP_ERRNO_PARM;
return -1;
}
/* Find out actual length of the built string */
va_start (ap, fmt);
#ifdef _WIN32
len = _vscprintf(fmt, ap) + 1;
#else
len = vsnprintf(NULL, 0, fmt, ap) + 1;
#endif
va_end(ap);
/* Deliver string */
if (len > 0)
{
*strp = (char *)mpmalloc(len);
if (*strp != NULL)
{
va_start (ap, fmt);
rc = vsprintf(*strp, fmt, ap);
va_end(ap);
}
}
return rc;
}
/****************************************************************************
|*
|* Function: mpasprintf_mpid
|*
|* Description;
|*
|* anolog to sprintf() except that allocates a string large enough to hold
|* the output.
|*
|* Return:
|* The number of bytes allocated on success
|* -1 on error and the content of strp is undefined on error.
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140815 JG Initial version
|*
****************************************************************************/
int mpasprintf_mpid(char **strp, int mpid, const char *fmt, ...)
{
int rc = 0;
int len = 0;
va_list ap;
/* Check parameters */
if (fmt == NULL || strp == NULL)
{
mperrno = MP_ERRNO_PARM;
return -1;
}
/* Find out actual length of the built string */
va_start (ap, fmt);
#ifdef _WIN32
len = _vscprintf(fmt, ap) + 1;
#else
len = vsnprintf(NULL, 0, fmt, ap) + 1;
#endif
va_end(ap);
/* Deliver string */
if (len > 0)
{
*strp = (char *)mpmalloc_mpid(len, mpid);
if (*strp != NULL)
{
va_start (ap, fmt);
rc = vsprintf(*strp, fmt, ap);
va_end(ap);
}
}
return rc;
}
/****************************************************************************
|*
|* Function: mpnew
|*
|* Description;
|*
|* Initializes the next memory pool available in the array of memory pools
|* and assignes the corresponding description
|*
|* Return:
|* the memory pool ID of the new allocated memory pool
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
int mpnew(char *descr)
{
int i = 0, mpid = MP_ERRNO_EXMP;
char *curr_descr = NULL;
mperrno = MP_ERRNO_SUCCESS;
/* Check parameter */
if (descr == NULL)
{
curr_descr = "-";
}
else
{
curr_descr = descr;
}
#if MP_THREAD_SAFE == 1 /* { */
# if _WIN32 /* { */
if ((mperrno = mp_mutex_init(&mp_mutex_p)) != MP_ERRNO_SUCCESS)
{
return mperrno;
}
# endif /* } _WIN32 */
MP_MUTEX_LOCK(mp_mutex_p); /* No UT but checked with helgrind */
#endif /* } MP_THREAD_SAFE */
/* Find next available memory pool */
for(i = 0; i < MP_MAX_MP_ID; i++)
{
if (mp_arr[i].init != 'Y')
{
if (i == MP_DEF_MP_ID)
{
continue;
}
mp_arr[i].init = 'Y';
strncpy(mp_arr[i].descr, curr_descr, sizeof(mp_arr[i].descr));
mp_arr[i].descr[sizeof(mp_arr[i].descr) -1] = '\0';
#if MP_THREAD_SAFE == 1
mp_arr[i].thread_id = MP_CURR_THREAD;
#endif
mp_arr[i].head_block = NULL;
mp_arr[i].tail_block = NULL;
mpid = i;
#ifndef MP_VALGRIND_NOT_AVAILABLE
VALGRIND_CREATE_MEMPOOL((void *)&mp_arr[mpid], 0, 1);
#endif
break;
}
}
#if MP_THREAD_SAFE == 1
MP_MUTEX_UNLOCK(mp_mutex_p);
#endif
mperrno = mpid == MP_ERRNO_EXMP ? MP_ERRNO_EXMP : mperrno;
return mpid;
}
/****************************************************************************
|*
|* Function: mppush
|*
|* Description;
|*
|* Moves the current memory pool ID to the stack and makes mpid the
|* current one. The stack has only one element, for now.
|*
|* Return:
|* MP_ERRNO_SUCCESS if success
|* MP_ERRNO_MPID, MP_ERRNO_NOIN if error
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
int mppush(int mpid)
{
mperrno = MP_ERRNO_SUCCESS;
/* Memory pool ID out of limit */
if (mpid > MP_MAX_MP_ID -1 || mpid < 0)
{
mperrno = MP_ERRNO_MPID;
return MP_ERRNO_MPID;
}
/* Memory pool ID not initiliazed */
if (mp_arr[mpid].init != 'Y' && mpid != MP_DEF_MP_ID)
{
mperrno = MP_ERRNO_NOIN;
return MP_ERRNO_NOIN;
}
/* Push */
mp_prev_mpid = mp_cur_mpid;
mp_cur_mpid = mpid;
return MP_ERRNO_SUCCESS;
}
/****************************************************************************
|*
|* Function: mppop
|*
|* Description;
|*
|* Sets the previous Memory Pool ID as the current one
|*
|* Return:
|* MP_ERRNO_SUCCESS if success
|* MP_ERRNO_NOPP if error
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
int mppop()
{
mperrno = MP_ERRNO_SUCCESS;
/* Nothing to pop */
if (mp_prev_mpid < 0 || mp_prev_mpid > MP_MAX_MP_ID -1)
{
mperrno = MP_ERRNO_NOPP;
return MP_ERRNO_NOPP;
}
/* Pop */
mp_cur_mpid = mp_prev_mpid;
mp_prev_mpid = MP_NO_MP_ID;
return MP_ERRNO_SUCCESS;
}
/****************************************************************************
|*
|* Function: mpset
|*
|* Description;
|*
|* Sets the current memory pool ID
|*
|* Return:
|* MP_ERRNO_SUCCESS if success
|* MP_ERRNO_MPID, MP_ERRNO_NOIN, MP_ERRNO_THRD if error
|*
|* Author: Javier Gutierrez (JG)
|*
|* Modifications:
|* 20140811 JG Initial version
|*
****************************************************************************/
int mpset(int mpid)
{
mperrno = MP_ERRNO_SUCCESS;
/* Memory pool ID out of limit */
if (mpid > MP_MAX_MP_ID -1 || mpid < 0)
{
mperrno = MP_ERRNO_MPID;
return MP_ERRNO_MPID;
}
/* Memory pool ID not initiliazed */
if (mp_arr[mpid].init != 'Y' && mpid != MP_DEF_MP_ID)
{
mperrno = MP_ERRNO_NOIN;
return MP_ERRNO_NOIN;
}
#if MP_THREAD_SAFE == 1
/* Check thread ID */
if (MP_THREAD_EQ(mp_arr[mpid].thread_id, MP_CURR_THREAD) == 0)
{
mperrno = MP_ERRNO_THRD;
return MP_ERRNO_THRD;
}
#endif