-
Notifications
You must be signed in to change notification settings - Fork 54
/
lib_mem.c
2865 lines (2508 loc) · 117 KB
/
lib_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
/*
*********************************************************************************************************
* uC/LIB
* Custom Library Modules
*
* Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* STANDARD MEMORY OPERATIONS
*
* Filename : lib_mem.c
* Version : V1.39.01
*********************************************************************************************************
* Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
*
* (a) ALL standard library functions are implemented in the custom library modules :
*
* (1) \<Custom Library Directory>\lib_*.*
*
* (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
*
* where
* <Custom Library Directory> directory path for custom library software
* <cpu> directory name for specific processor (CPU)
* <compiler> directory name for specific compiler
*
* (b) Product-specific library functions are implemented in individual products.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#define LIB_MEM_MODULE
#include "lib_mem.h"
#include "lib_math.h"
#include "lib_str.h"
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_HEAP_SIZE > 0u)
#ifndef LIB_MEM_CFG_HEAP_BASE_ADDR
CPU_INT08U Mem_Heap[LIB_MEM_CFG_HEAP_SIZE]; /* Mem heap. */
#endif
MEM_SEG Mem_SegHeap; /* Heap mem seg. */
#endif
MEM_SEG *Mem_SegHeadPtr; /* Ptr to head of seg list. */
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void Mem_SegCreateCritical (const CPU_CHAR *p_name,
MEM_SEG *p_seg,
CPU_ADDR seg_base_addr,
CPU_SIZE_T padding_align,
CPU_SIZE_T size);
#if (LIB_MEM_CFG_HEAP_SIZE > 0u)
static MEM_SEG *Mem_SegOverlapChkCritical( CPU_ADDR seg_base_addr,
CPU_SIZE_T size,
LIB_ERR *p_err);
#endif
static void *Mem_SegAllocInternal (const CPU_CHAR *p_name,
MEM_SEG *p_seg,
CPU_SIZE_T size,
CPU_SIZE_T align,
CPU_SIZE_T padding_align,
CPU_SIZE_T *p_bytes_reqd,
LIB_ERR *p_err);
static void *Mem_SegAllocExtCritical ( MEM_SEG *p_seg,
CPU_SIZE_T size,
CPU_SIZE_T align,
CPU_SIZE_T padding_align,
CPU_SIZE_T *p_bytes_reqd,
LIB_ERR *p_err);
static void Mem_DynPoolCreateInternal(const CPU_CHAR *p_name,
MEM_DYN_POOL *p_pool,
MEM_SEG *p_seg,
CPU_SIZE_T blk_size,
CPU_SIZE_T blk_align,
CPU_SIZE_T blk_padding_align,
CPU_SIZE_T blk_qty_init,
CPU_SIZE_T blk_qty_max,
LIB_ERR *p_err);
#if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
static void Mem_SegAllocTrackCritical(const CPU_CHAR *p_name,
MEM_SEG *p_seg,
CPU_SIZE_T size,
LIB_ERR *p_err);
#endif
#if ((LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) && \
(LIB_MEM_CFG_HEAP_SIZE > 0u))
static CPU_BOOLEAN Mem_PoolBlkIsValidAddr ( MEM_POOL *p_pool,
void *p_mem);
#endif
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
* GLOBAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* Mem_Init()
*
* Description : (1) Initializes Memory Management Module :
*
* (a) Initialize heap memory pool
* (b) Initialize memory pool table
*
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (2) Mem_Init() MUST be called ... :
*
* (a) ONLY ONCE from a product's application; ...
* (b) BEFORE product's application calls any memory library module function(s)
*********************************************************************************************************
*/
void Mem_Init (void)
{
/* ------------------ INIT SEG LIST ------------------- */
Mem_SegHeadPtr = DEF_NULL;
#if (LIB_MEM_CFG_HEAP_SIZE > 0u)
{
LIB_ERR err;
CPU_ADDR heap_base_addr;
/* ------------------ INIT HEAP SEG ------------------- */
#ifdef LIB_MEM_CFG_HEAP_BASE_ADDR
heap_base_addr = LIB_MEM_CFG_HEAP_BASE_ADDR;
#else
heap_base_addr = (CPU_ADDR)&Mem_Heap[0u];
#endif
Mem_SegCreate("Heap",
&Mem_SegHeap, /* Create heap seg. */
heap_base_addr,
LIB_MEM_CFG_HEAP_SIZE,
LIB_MEM_PADDING_ALIGN_NONE,
&err);
if (err != LIB_MEM_ERR_NONE) {
CPU_SW_EXCEPTION(;);
}
}
#endif
}
/*
*********************************************************************************************************
* Mem_Clr()
*
* Description : Clears data buffer (see Note #2).
*
* Argument(s) : pmem Pointer to memory buffer to clear.
*
* size Number of data buffer octets to clear (see Note #1).
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null clears allowed (i.e. zero-length clears).
*
* See also 'Mem_Set() Note #1'.
*
* (2) Clear data by setting each data octet to 0.
*********************************************************************************************************
*/
void Mem_Clr (void *pmem,
CPU_SIZE_T size)
{
Mem_Set(pmem,
0u, /* See Note #2. */
size);
}
/*
*********************************************************************************************************
* Mem_Set()
*
* Description : Fills data buffer with specified data octet.
*
* Argument(s) : pmem Pointer to memory buffer to fill with specified data octet.
*
* data_val Data fill octet value.
*
* size Number of data buffer octets to fill (see Note #1).
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null sets allowed (i.e. zero-length sets).
*
* (2) For best CPU performance, optimized to fill data buffer using 'CPU_ALIGN'-sized data
* words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
* word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
* addresses.
*
* (3) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
* address boundary.
*
* Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
* address values MUST be cast to an appropriately-sized integer value PRIOR to any
* 'mem_align_mod' arithmetic operation.
*********************************************************************************************************
*/
void Mem_Set (void *pmem,
CPU_INT08U data_val,
CPU_SIZE_T size)
{
CPU_SIZE_T size_rem;
CPU_ALIGN data_align;
CPU_ALIGN *pmem_align;
CPU_INT08U *pmem_08;
CPU_DATA mem_align_mod;
CPU_DATA i;
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (size < 1) { /* See Note #1. */
return;
}
if (pmem == (void *)0) {
return;
}
#endif
data_align = 0u;
for (i = 0u; i < sizeof(CPU_ALIGN); i++) { /* Fill each data_align octet with data val. */
data_align <<= DEF_OCTET_NBR_BITS;
data_align |= (CPU_ALIGN)data_val;
}
size_rem = size;
mem_align_mod = (CPU_INT08U)((CPU_ADDR)pmem % sizeof(CPU_ALIGN)); /* See Note #3. */
pmem_08 = (CPU_INT08U *)pmem;
if (mem_align_mod != 0u) { /* If leading octets avail, ... */
i = mem_align_mod;
while ((size_rem > 0) && /* ... start mem buf fill with leading octets ... */
(i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
*pmem_08++ = data_val;
size_rem -= sizeof(CPU_INT08U);
i++;
}
}
pmem_align = (CPU_ALIGN *)pmem_08; /* See Note #2. */
while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem buf aligned on CPU_ALIGN word boundaries, */
*pmem_align++ = data_align; /* ... fill mem buf with CPU_ALIGN-sized data. */
size_rem -= sizeof(CPU_ALIGN);
}
pmem_08 = (CPU_INT08U *)pmem_align;
while (size_rem > 0) { /* Finish mem buf fill with trailing octets. */
*pmem_08++ = data_val;
size_rem -= sizeof(CPU_INT08U);
}
}
/*
*********************************************************************************************************
* Mem_Copy()
*
* Description : Copies data octets from one memory buffer to another memory buffer.
*
* Argument(s) : pdest Pointer to destination memory buffer.
*
* psrc Pointer to source memory buffer.
*
* size Number of octets to copy (see Note #1).
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null copies allowed (i.e. zero-length copies).
*
* (2) Memory buffers NOT checked for overlapping.
*
* (a) IEEE Std 1003.1, 2004 Edition, Section 'memcpy() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined".
*
* (b) However, data octets from a source memory buffer at a higher address value SHOULD
* successfully copy to a destination memory buffer at a lower address value even
* if any octets of the memory buffers overlap as long as no individual, atomic CPU
* word copy overlaps.
*
* Since Mem_Copy() performs the data octet copy via 'CPU_ALIGN'-sized words &/or
* octets; & since 'CPU_ALIGN'-sized words MUST be accessed on word-aligned addresses
* (see Note #3b), neither 'CPU_ALIGN'-sized words nor octets at unique addresses can
* ever overlap.
*
* Therefore, Mem_Copy() SHOULD be able to successfully copy overlapping memory
* buffers as long as the source memory buffer is at a higher address value than the
* destination memory buffer.
*
* (3) For best CPU performance, optimized to copy data buffer using 'CPU_ALIGN'-sized data
* words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
* word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
* addresses.
*
* (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
* address boundary.
*
* Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
* address values MUST be cast to an appropriately-sized integer value PRIOR to any
* 'mem_align_mod' arithmetic operation.
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_OPTIMIZE_ASM_EN != DEF_ENABLED)
void Mem_Copy ( void *pdest,
const void *psrc,
CPU_SIZE_T size)
{
CPU_SIZE_T size_rem;
CPU_SIZE_T mem_gap_octets;
CPU_ALIGN *pmem_align_dest;
const CPU_ALIGN *pmem_align_src;
CPU_INT08U *pmem_08_dest;
const CPU_INT08U *pmem_08_src;
CPU_DATA i;
CPU_DATA mem_align_mod_dest;
CPU_DATA mem_align_mod_src;
CPU_BOOLEAN mem_aligned;
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (size < 1) { /* See Note #1. */
return;
}
if (pdest == (void *)0) {
return;
}
if (psrc == (void *)0) {
return;
}
#endif
size_rem = size;
pmem_08_dest = ( CPU_INT08U *)pdest;
pmem_08_src = (const CPU_INT08U *)psrc;
mem_gap_octets = (CPU_SIZE_T)(pmem_08_src - pmem_08_dest);
if (mem_gap_octets >= sizeof(CPU_ALIGN)) { /* Avoid bufs overlap. */
/* See Note #4. */
mem_align_mod_dest = (CPU_INT08U)((CPU_ADDR)pmem_08_dest % sizeof(CPU_ALIGN));
mem_align_mod_src = (CPU_INT08U)((CPU_ADDR)pmem_08_src % sizeof(CPU_ALIGN));
mem_aligned = (mem_align_mod_dest == mem_align_mod_src) ? DEF_YES : DEF_NO;
if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
/* ... optimize copy for mem buf alignment. */
if (mem_align_mod_dest != 0u) { /* If leading octets avail, ... */
i = mem_align_mod_dest;
while ((size_rem > 0) && /* ... start mem buf copy with leading octets ... */
(i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
*pmem_08_dest++ = *pmem_08_src++;
size_rem -= sizeof(CPU_INT08U);
i++;
}
}
pmem_align_dest = ( CPU_ALIGN *)pmem_08_dest; /* See Note #3. */
pmem_align_src = (const CPU_ALIGN *)pmem_08_src;
while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem bufs aligned on CPU_ALIGN word boundaries, */
*pmem_align_dest++ = *pmem_align_src++; /* ... copy psrc to pdest with CPU_ALIGN-sized words. */
size_rem -= sizeof(CPU_ALIGN);
}
pmem_08_dest = ( CPU_INT08U *)pmem_align_dest;
pmem_08_src = (const CPU_INT08U *)pmem_align_src;
}
}
while (size_rem > 0) { /* For unaligned mem bufs or trailing octets, ... */
*pmem_08_dest++ = *pmem_08_src++; /* ... copy psrc to pdest by octets. */
size_rem -= sizeof(CPU_INT08U);
}
}
#endif
/*
*********************************************************************************************************
* Mem_Move()
*
* Description : Moves data octets from one memory buffer to another memory buffer, or within the same
* memory buffer. Overlapping is correctly handled for all move operations.
*
* Argument(s) : pdest Pointer to destination memory buffer.
*
* psrc Pointer to source memory buffer.
*
* size Number of octets to move (see Note #1).
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null move operations allowed (i.e. zero-length).
*
* (2) Memory buffers checked for overlapping.
*
* (3) For best CPU performance, optimized to copy data buffer using 'CPU_ALIGN'-sized data
* words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
* word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
* addresses.
*
* (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
* address boundary.
*
* Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
* address values MUST be cast to an appropriately-sized integer value PRIOR to any
* 'mem_align_mod' arithmetic operation.
*********************************************************************************************************
*/
void Mem_Move ( void *pdest,
const void *psrc,
CPU_SIZE_T size)
{
CPU_SIZE_T size_rem;
CPU_SIZE_T mem_gap_octets;
CPU_ALIGN *pmem_align_dest;
const CPU_ALIGN *pmem_align_src;
CPU_INT08U *pmem_08_dest;
const CPU_INT08U *pmem_08_src;
CPU_INT08S i;
CPU_DATA mem_align_mod_dest;
CPU_DATA mem_align_mod_src;
CPU_BOOLEAN mem_aligned;
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (size < 1) {
return;
}
if (pdest == (void *)0) {
return;
}
if (psrc == (void *)0) {
return;
}
#endif
pmem_08_src = (const CPU_INT08U *)psrc;
pmem_08_dest = ( CPU_INT08U *)pdest;
if (pmem_08_src > pmem_08_dest) {
Mem_Copy(pdest, psrc, size);
return;
}
size_rem = size;
pmem_08_dest = ( CPU_INT08U *)pdest + size - 1;
pmem_08_src = (const CPU_INT08U *)psrc + size - 1;
mem_gap_octets = (CPU_SIZE_T)(pmem_08_dest - pmem_08_src);
if (mem_gap_octets >= sizeof(CPU_ALIGN)) { /* Avoid bufs overlap. */
/* See Note #4. */
mem_align_mod_dest = (CPU_INT08U)((CPU_ADDR)pmem_08_dest % sizeof(CPU_ALIGN));
mem_align_mod_src = (CPU_INT08U)((CPU_ADDR)pmem_08_src % sizeof(CPU_ALIGN));
mem_aligned = (mem_align_mod_dest == mem_align_mod_src) ? DEF_YES : DEF_NO;
if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
/* ... optimize copy for mem buf alignment. */
if (mem_align_mod_dest != (sizeof(CPU_ALIGN) - 1)) {/* If leading octets avail, ... */
i = (CPU_INT08S)mem_align_mod_dest;
while ((size_rem > 0) && /* ... start mem buf copy with leading octets ... */
(i >= 0)) { /* ... until next CPU_ALIGN word boundary. */
*pmem_08_dest-- = *pmem_08_src--;
size_rem -= sizeof(CPU_INT08U);
i--;
}
}
/* See Note #3. */
pmem_align_dest = ( CPU_ALIGN *)(((CPU_INT08U *)pmem_08_dest - sizeof(CPU_ALIGN)) + 1);
pmem_align_src = (const CPU_ALIGN *)(((CPU_INT08U *)pmem_08_src - sizeof(CPU_ALIGN)) + 1);
while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem bufs aligned on CPU_ALIGN word boundaries, */
*pmem_align_dest-- = *pmem_align_src--; /* ... copy psrc to pdest with CPU_ALIGN-sized words. */
size_rem -= sizeof(CPU_ALIGN);
}
pmem_08_dest = ( CPU_INT08U *)pmem_align_dest + sizeof(CPU_ALIGN) - 1;
pmem_08_src = (const CPU_INT08U *)pmem_align_src + sizeof(CPU_ALIGN) - 1;
}
}
while (size_rem > 0) { /* For unaligned mem bufs or trailing octets, ... */
*pmem_08_dest-- = *pmem_08_src--; /* ... copy psrc to pdest by octets. */
size_rem -= sizeof(CPU_INT08U);
}
}
/*
*********************************************************************************************************
* Mem_Cmp()
*
* Description : Verifies that ALL data octets in two memory buffers are identical in sequence.
*
* Argument(s) : p1_mem Pointer to first memory buffer.
*
* p2_mem Pointer to second memory buffer.
*
* size Number of data buffer octets to compare (see Note #1).
*
* Return(s) : DEF_YES, if 'size' number of data octets are identical in both memory buffers.
*
* DEF_NO, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null compares allowed (i.e. zero-length compares); 'DEF_YES' returned to indicate
* identical null compare.
*
* (2) Many memory buffer comparisons vary ONLY in the least significant octets -- e.g.
* network address buffers. Consequently, memory buffer comparison is more efficient
* if the comparison starts from the end of the memory buffers which will abort sooner
* on dissimilar memory buffers that vary only in the least significant octets.
*
* (3) For best CPU performance, optimized to compare data buffers using 'CPU_ALIGN'-sized
* data words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
* word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
* addresses.
*
* (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
* address boundary.
*
* Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
* address values MUST be cast to an appropriately-sized integer value PRIOR to any
* 'mem_align_mod' arithmetic operation.
*********************************************************************************************************
*/
CPU_BOOLEAN Mem_Cmp (const void *p1_mem,
const void *p2_mem,
CPU_SIZE_T size)
{
CPU_SIZE_T size_rem;
CPU_ALIGN *p1_mem_align;
CPU_ALIGN *p2_mem_align;
const CPU_INT08U *p1_mem_08;
const CPU_INT08U *p2_mem_08;
CPU_DATA i;
CPU_DATA mem_align_mod_1;
CPU_DATA mem_align_mod_2;
CPU_BOOLEAN mem_aligned;
CPU_BOOLEAN mem_cmp;
if (size < 1) { /* See Note #1. */
return (DEF_YES);
}
if (p1_mem == (void *)0) {
return (DEF_NO);
}
if (p2_mem == (void *)0) {
return (DEF_NO);
}
mem_cmp = DEF_YES; /* Assume mem bufs are identical until cmp fails. */
size_rem = size;
/* Start @ end of mem bufs (see Note #2). */
p1_mem_08 = (const CPU_INT08U *)p1_mem + size;
p2_mem_08 = (const CPU_INT08U *)p2_mem + size;
/* See Note #4. */
mem_align_mod_1 = (CPU_INT08U)((CPU_ADDR)p1_mem_08 % sizeof(CPU_ALIGN));
mem_align_mod_2 = (CPU_INT08U)((CPU_ADDR)p2_mem_08 % sizeof(CPU_ALIGN));
mem_aligned = (mem_align_mod_1 == mem_align_mod_2) ? DEF_YES : DEF_NO;
if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
/* ... optimize cmp for mem buf alignment. */
if (mem_align_mod_1 != 0u) { /* If trailing octets avail, ... */
i = mem_align_mod_1;
while ((mem_cmp == DEF_YES) && /* ... cmp mem bufs while identical & ... */
(size_rem > 0) && /* ... start mem buf cmp with trailing octets ... */
(i > 0)) { /* ... until next CPU_ALIGN word boundary. */
p1_mem_08--;
p2_mem_08--;
if (*p1_mem_08 != *p2_mem_08) { /* If ANY data octet(s) NOT identical, cmp fails. */
mem_cmp = DEF_NO;
}
size_rem -= sizeof(CPU_INT08U);
i--;
}
}
if (mem_cmp == DEF_YES) { /* If cmp still identical, cmp aligned mem bufs. */
p1_mem_align = (CPU_ALIGN *)p1_mem_08; /* See Note #3. */
p2_mem_align = (CPU_ALIGN *)p2_mem_08;
while ((mem_cmp == DEF_YES) && /* Cmp mem bufs while identical & ... */
(size_rem >= sizeof(CPU_ALIGN))) { /* ... mem bufs aligned on CPU_ALIGN word boundaries. */
p1_mem_align--;
p2_mem_align--;
if (*p1_mem_align != *p2_mem_align) { /* If ANY data octet(s) NOT identical, cmp fails. */
mem_cmp = DEF_NO;
}
size_rem -= sizeof(CPU_ALIGN);
}
p1_mem_08 = (CPU_INT08U *)p1_mem_align;
p2_mem_08 = (CPU_INT08U *)p2_mem_align;
}
}
while ((mem_cmp == DEF_YES) && /* Cmp mem bufs while identical ... */
(size_rem > 0)) { /* ... for unaligned mem bufs or trailing octets. */
p1_mem_08--;
p2_mem_08--;
if (*p1_mem_08 != *p2_mem_08) { /* If ANY data octet(s) NOT identical, cmp fails. */
mem_cmp = DEF_NO;
}
size_rem -= sizeof(CPU_INT08U);
}
return (mem_cmp);
}
/*
*********************************************************************************************************
* Mem_HeapAlloc()
*
* Description : Allocates a memory block from the heap memory segment.
*
* Argument(s) : size Size of memory block to allocate (in bytes).
*
* align Alignment of memory block to specific word boundary (in bytes).
*
* p_bytes_reqd Optional pointer to a variable to ... :
*
* (a) Return the number of bytes required to successfully
* allocate the memory block, if any error(s);
* (b) Return 0, otherwise.
*
* p_err Pointer to variable that will receive the return error code from this function :
*
* LIB_MEM_ERR_NONE Operation was successful.
* LIB_MEM_ERR_HEAP_EMPTY No more memory available on heap.
*
* ---------------------RETURNED BY Mem_SegAllocInternal()---------------------
* LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
* LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
* LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
*
* Return(s) : Pointer to memory block, if NO error(s).
*
* Pointer to NULL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) Pointers to variables that return values MUST be initialized PRIOR to all other
* validation or function handling in case of any error(s).
*
* (2) This function is DEPRECATED and will be removed in a future version of this product.
* Mem_SegAlloc(), Mem_SegAllocExt() or Mem_SegAllocHW() should be used instead.
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_HEAP_SIZE > 0u)
void *Mem_HeapAlloc (CPU_SIZE_T size,
CPU_SIZE_T align,
CPU_SIZE_T *p_bytes_reqd,
LIB_ERR *p_err)
{
void *p_mem;
p_mem = Mem_SegAllocInternal(DEF_NULL,
&Mem_SegHeap,
size,
align,
LIB_MEM_CFG_HEAP_PADDING_ALIGN,
p_bytes_reqd,
p_err);
if (*p_err == LIB_MEM_ERR_SEG_OVF) {
*p_err = LIB_MEM_ERR_HEAP_OVF;
}
return (p_mem);
}
#endif
/*
*********************************************************************************************************
* Mem_HeapGetSizeRem()
*
* Description : Gets remaining heap memory size available to allocate.
*
* Argument(s) : align Desired word boundary alignment (in bytes) to return remaining memory size from.
*
* p_err Pointer to variable that will receive the return error code from this function
*
* LIB_MEM_ERR_NONE Operation was successful.
*
* --------------------RETURNED BY Mem_SegRemSizeGet()--------------------
* LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.
* LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory alignment.
*
* Return(s) : Remaining heap memory size (in bytes), if NO error(s).
*
* 0, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) This function is DEPRECATED and will be removed in a future version of this product.
* Mem_SegRemSizeGet() should be used instead.
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_HEAP_SIZE > 0u)
CPU_SIZE_T Mem_HeapGetSizeRem (CPU_SIZE_T align,
LIB_ERR *p_err)
{
CPU_SIZE_T rem_size;
rem_size = Mem_SegRemSizeGet(&Mem_SegHeap,
align,
DEF_NULL,
p_err);
if (*p_err != LIB_MEM_ERR_NONE) {
return (0u);
}
return (rem_size);
}
#endif
/*
*********************************************************************************************************
* Mem_SegCreate()
*
* Description : Creates a new memory segment to be used for runtime memory allocation.
*
* Argument(s) : p_name Pointer to segment name.
*
* p_seg Pointer to segment data. Must be allocated by caller.
*
* seg_base_addr Address of segment's first byte.
*
* size Total size of segment, in bytes.
*
* padding_align Padding alignment, in bytes, that will be added to any allocated buffer from
* this memory segment. MUST be a power of 2. LIB_MEM_PADDING_ALIGN_NONE
* means no padding.
*
* p_err Pointer to variable that will receive the return error code from this function :
*
* LIB_MEM_ERR_NONE Operation was successful.
* LIB_MEM_ERR_INVALID_SEG_SIZE Invalid segment size specified.
* LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid padding alignment.
* LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
*
* -------------------RETURNED BY Mem_SegOverlapChkCritical()-------------------
* LIB_MEM_ERR_INVALID_SEG_OVERLAP Segment overlaps another existing segment.
* LIB_MEM_ERR_INVALID_SEG_EXISTS Segment already exists.
*
* Return(s) : None.
*
* Caller(s) : Application.
*
* Note(s) : (1) New segments are checked for overlap with existing segments. A critical section needs
* to be maintained during the whole list search and add procedure to prevent a reentrant
* call from creating another segment overlapping with the one being added.
*********************************************************************************************************
*/
void Mem_SegCreate (const CPU_CHAR *p_name,
MEM_SEG *p_seg,
CPU_ADDR seg_base_addr,
CPU_SIZE_T size,
CPU_SIZE_T padding_align,
LIB_ERR *p_err)
{
CPU_SR_ALLOC();
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (p_err == DEF_NULL) { /* Chk for null err ptr. */
CPU_SW_EXCEPTION(;);
}
if (p_seg == DEF_NULL) { /* Chk for null seg ptr. */
*p_err = LIB_MEM_ERR_NULL_PTR;
return;
}
if (size < 1u) { /* Chk for invalid sized seg. */
*p_err = LIB_MEM_ERR_INVALID_SEG_SIZE;
return;
}
/* Chk for addr space ovf. */
if (seg_base_addr + (size - 1u) < seg_base_addr) {
*p_err = LIB_MEM_ERR_INVALID_SEG_SIZE;
return;
}
if ((padding_align != LIB_MEM_PADDING_ALIGN_NONE) &&
(MATH_IS_PWR2(padding_align) != DEF_YES)) {
*p_err = LIB_MEM_ERR_INVALID_MEM_ALIGN;
return;
}
#endif
CPU_CRITICAL_ENTER();
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) && \
(LIB_MEM_CFG_HEAP_SIZE > 0u)
(void)Mem_SegOverlapChkCritical(seg_base_addr, /* Chk for overlap. */
size,
p_err);
if (*p_err != LIB_MEM_ERR_NONE) {
CPU_CRITICAL_EXIT();
return;
}
#endif
Mem_SegCreateCritical(p_name, /* Create seg. */
p_seg,
seg_base_addr,
padding_align,
size);
CPU_CRITICAL_EXIT();
*p_err = LIB_MEM_ERR_NONE;
}
/*
*********************************************************************************************************
* Mem_SegClr()
*
* Description : Clears a memory segment.
*
* Argument(s) : p_seg Pointer to segment data. Must be allocated by caller.
*
* p_err Pointer to variable that will receive the return error code from this function :
*
* LIB_MEM_ERR_NONE Operation was successful.
* LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.
*
* Return(s) : None.
*
* Caller(s) : Application.
*
* Note(s) : (1) This function must be used with extreme caution. It must only be called on memory
* segments that are no longer used.
*
* (2) This function is disabled when debug mode is enabled to avoid heap memory leaks.
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_DBG_INFO_EN == DEF_DISABLED)
void Mem_SegClr (MEM_SEG *p_seg,
LIB_ERR *p_err)
{
CPU_SR_ALLOC();
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (p_err == DEF_NULL) { /* Chk for null err ptr. */
CPU_SW_EXCEPTION(;);
}
if (p_seg == DEF_NULL) { /* Chk for null seg ptr. */
*p_err = LIB_MEM_ERR_NULL_PTR;
return;
}
#endif
CPU_CRITICAL_ENTER();
p_seg->AddrNext = p_seg->AddrBase;
CPU_CRITICAL_EXIT();
*p_err = LIB_MEM_ERR_NONE;
}
#endif
/*
*********************************************************************************************************
* Mem_SegRemSizeGet()
*
* Description : Gets free space of memory segment.
*
* Argument(s) : p_seg Pointer to segment data.
*
* align Alignment in bytes to assume for calculation of free space.
*
* p_seg_info Pointer to structure that will receive further segment info data (used size,
* total size, base address and next allocation address).
*
* p_err Pointer to variable that will receive the return error code from this function :
*
* LIB_MEM_ERR_NONE Operation was successful.
* LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.