Init basesystem source codes.
[staging/basesystem.git] / vehicleservice / positioning_base_library / library / src / _pbMem.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
19  File name      : _pbMem.cpp
20  System name    : 05 Integration Platform
21  Subsystem name : System common functions
22  Title          : System API
23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
24 */
25
26 #include <fcntl.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <vehicle_service/positioning_base_library.h>
30 #include "tchar.h"
31 #include "WPF_STD_private.h"
32 #include "_pbInternalProc.h"
33
34 /*
35  Constants and data type definitions
36 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
37 #define MAX_AREA_NAME_LEN        32
38 #define MAX_FILENAME_LEN        32
39 #define _CWORD64__MEM_MUTEX_NAME        __TEXT("POS_BASE_MEM_MUTEX")
40 #define MAX_ROMFILENAME_LEN        (32-4)        /* To the name appended with "_rom" at the time of registration */
41 #define PHYADDR_MAX                0x9FFFFFFF    /* Physical address of the boundary divided by 256 */
42 #define ROM_DATA_SIZE            0x00400000    /* Size of the virtual ROM data area(4MB) */
43 #define ALLOC_SIZE                0x00200000    /* 2MB (Minimum VirtualAlloc allocation of shared memory, */
44 /*      which is less than 2MB, is not mapped to shared memory) */
45 #define OFFSET_SIZE                0x00001000    /* 4KB (VirtualCopy Addressing Units) */
46
47   /* Memory map information */
48 typedef struct TagMemMapItem {
49     HANDLE                    h_heap;             /* Handle of heap.           */
50     struct TagMemMapItem*     p_next;             /* Pointers to the next Chain memory map table    */
51     struct TagMemMapItem*     p_prev;             /* Pointers to the previous Chain memory map table    */
52     DWORD                     address;
53     TCHAR                     name[MAX_AREA_NAME_LEN];    /* Name of shared data area(Not used when permit is processed)        */
54     HANDLE                    h_shared_mem;                    /* Shared memory handle           */
55 } MEMMAP_ITEM;
56
57 /* Memory map information management table */
58 typedef struct {
59     HANDLE                    h_heap;            /* Heap handle of the invoking table                */
60     MEMMAP_ITEM*              p_head;            /* Pointer to the first memory map information            */
61     MEMMAP_ITEM*              p_tail;            /* Pointer to the terminating memory map information            */
62     DWORD                     num_of_items;        /* Chain memory map data                */
63     HANDLE                    h_mutex;            /* _CWORD64__MEM_MUTEX Exclusive Mutex handles    */
64 } MEMMAP_LIST;
65
66 typedef struct {
67     DWORD                    mem_size;
68     DWORD                    reserv[3];
69 } _CWORD64_SHMHDR;
70
71 /* LINK ROM data top address/size storage ROM data */
72 typedef struct {
73     u_int32                    *link_rom_addr;    /* ROM data start address                        */
74     u_int32                    link_size;        /* ROM data size                                */
75 } LINK_ROM;
76
77 /*
78  Internal function prototype declaration
79 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
80 static void            UnlinkFromMemMapList(MEMMAP_ITEM* p_item);
81 static void            LinkToMemMapList(MEMMAP_ITEM* p_item);
82 static MEMMAP_ITEM*        FindMemMapItemByName(TCHAR* name);
83
84 /*
85  Global variable
86 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
87 MEMMAP_LIST*    g_p_mem_map_list;
88
89 /**
90  * @brief
91  *   Memory API initialization
92  *
93  * @param[in]  none
94  *
95  * @return RET_API    RET_NORMAL Normal completion<br>
96  *                        RET_ERROR     ABENDs
97  */
98
99 RET_API MemoryInit(void) {
100     RET_API        ret_api = RET_NORMAL;
101     TCHAR        name[] = _CWORD64__MEM_MUTEX_NAME;
102
103     // LCOV_EXCL_BR_START 5: standard lib error
104     g_p_mem_map_list = reinterpret_cast<MEMMAP_LIST *>(PbProcessHeapAlloc(0, sizeof(MEMMAP_ITEM)));
105     // LCOV_EXCL_BR_STOP
106     if (g_p_mem_map_list == NULL) {  // LCOV_EXCL_BR_LINE 5: standard lib error
107         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
108         ret_api = RET_ERROR;  // LCOV_EXCL_LINE 5: standard lib error
109     } else {
110         /* Initialization of memory map information management table */
111         g_p_mem_map_list->h_heap = NULL;
112         g_p_mem_map_list->p_head = NULL;
113         g_p_mem_map_list->p_tail = NULL;
114         g_p_mem_map_list->num_of_items = 0;
115
116         /* Create Mutex */
117         g_p_mem_map_list->h_mutex = _pb_CreateMutex(NULL, TRUE, name);  // LCOV_EXCL_BR_LINE 200: no branch
118         if (g_p_mem_map_list->h_mutex == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be NULL
119             // LCOV_EXCL_START 200:dead code
120             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
121             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_CreateMutex ERROR [name:%s]", name);
122             _pb_Exit();    /* System recovery processing(Exception execution) */
123             // LCOV_EXCL_STOP
124         }
125     }
126
127     return ret_api;
128 }
129
130 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
131  * MODULE    : MemoryTerm
132  * ABSTRACT  : Memory API termination processing
133  * NOTE      : This function frees each allocated object.
134  *           : This function must be called only once by DllMain() at process termination.
135  * ARGUMENT  : None
136  * RETURN    : Return RET_NORMAL
137  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
138 RET_API
139 MemoryTerm(void) {  // LCOV_EXCL_START 8:dead code
140     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
141     RET_API        ret_api = RET_NORMAL;
142
143     if (g_p_mem_map_list == NULL) {
144         ret_api = RET_ERROR;
145     } else {
146         /* Lock Mutex */
147         PbMutexLock(g_p_mem_map_list->h_mutex, INFINITE);
148
149         while (g_p_mem_map_list->num_of_items > 0) {
150             MEMMAP_ITEM* p_item = g_p_mem_map_list->p_head;
151             if (g_p_mem_map_list->num_of_items == 1) {
152                 /* Delete last one of memory map list */
153                 g_p_mem_map_list->p_head = NULL;
154                 g_p_mem_map_list->p_tail = NULL;
155                 g_p_mem_map_list->num_of_items = 0;
156             } else {
157                 /* Deletes the beginning of the memory map list, with the next at the beginning. */
158                 g_p_mem_map_list->p_head = g_p_mem_map_list->p_head->p_next;
159                 g_p_mem_map_list->p_head->p_prev = NULL;
160                 g_p_mem_map_list->num_of_items--;
161             }
162
163             /* Releases the real memory specified in the memory map list. */
164             if (p_item != NULL) {
165                 PbProcessHeapFree(0, p_item);
166                 p_item = NULL;
167             }
168         }
169
170         /* Release Mutex */
171         PbMutexUnlock(g_p_mem_map_list->h_mutex);
172
173         /* Delete Mutex */
174         PbDeleteMutex(g_p_mem_map_list->h_mutex);
175
176         /* Free MEMMAP_LIST structure */
177         PbProcessHeapFree(0, g_p_mem_map_list);
178         g_p_mem_map_list = NULL;
179     }
180
181     return ret_api;
182 }
183 // LCOV_EXCL_STOP
184
185 /**
186  * @brief
187  *    Allocate Process Heap
188  *
189  *   Allocates a memory block from the process heap
190  *
191  * @param[in]    DWORD    dw_flags    Heap allocation scheme
192  * @param[in]    SIZE_T    dw_bytes    Number of bytes to allocate
193  *
194  * @return    LPVOID    Except NULL    Pointer to the allocated memory block
195  *                        NULL        Assignment Failed
196  */
197 LPVOID PbProcessHeapAlloc(DWORD dw_flags, SIZE_T dw_bytes) {
198     LPVOID        pv_ret = NULL;
199
200     if ((dw_flags & HEAP_ZERO_MEMORY) == 0) {  // LCOV_EXCL_BR_LINE 200: dw_flags cannot have bit HEAP_ZERO_MEMORY
201         /* Zero initialization not specified */
202         pv_ret = malloc(dw_bytes);
203     } else {
204         /* Zero initialization specified */
205         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
206         pv_ret = calloc(dw_bytes, sizeof(char));  // LCOV_EXCL_LINE 200: dw_flags cannot have bit HEAP_ZERO_MEMORY
207     }
208
209     return pv_ret;
210 }
211
212 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
213  * MODULE    : PrivateHeapAlloc
214  * ABSTRACT  : Allocate a block of memory from the private heap
215  * NOTE      : Because there is no private-heap notion in PosixBasedOS001,
216  *             Allocate from the process-heap using ProcessHeapAlloc
217  * ARGUMENT  : DWORD    dw_flags        Controlling Heap Allocation Methods
218  *           : SIZE_T    dw_bytes        Number of bytes to allocate
219  * RETURN    : LPVOID    Except NULL    Pointer to the allocated memory block
220  *           :             NULL        Assignment Failed
221  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
222 LPVOID
223 PrivateHeapAlloc(DWORD dw_flags, SIZE_T dw_bytes) {  // LCOV_EXCL_START 8:dead code
224     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
225     return PbProcessHeapAlloc(dw_flags, dw_bytes);
226 }
227 // LCOV_EXCL_STOP
228
229 /**
230  * @brief
231  *    Free Process Heap
232  *
233  *   Frees memory blocks allocated from the process heap
234  *
235  * @param[in]    DWORD    dw_flags    Heap free method(Unused)
236  * @param[in]    LPVOID    lp_mem        Pointer to the memory to be freed
237  *
238  * @return    BOOL            Non-zero    Normal completion
239  *                               0        ABENDs
240  */
241 BOOL PbProcessHeapFree(DWORD dw_flags, LPVOID lp_mem) {
242     free(lp_mem);
243
244     return TRUE;
245 }
246
247 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
248  * MODULE    : PrivateHeapFree
249  * ABSTRACT  : Free memory blocks allocated from private heap
250  * NOTE      : Because there is no private-heap notion in PosixBasedOS001,
251  *             Open using ProcessHeapFree
252  * ARGUMENT  : DWORD    dw_flags    Heap release option(Not used by PosixBasedOS001)
253  *           : LPVOID    lp_mem    Pointer to the memory to be freed
254  * RETURN    : BOOL        Non-zero    Normal
255  *           :             0        Abnormality
256  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
257 BOOL
258 PrivateHeapFree(DWORD dw_flags, LPVOID lp_mem) {  // LCOV_EXCL_START 8:dead code
259     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
260     return PbProcessHeapFree(dw_flags, lp_mem);
261 }
262 // LCOV_EXCL_STOP
263
264 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
265  * MODULE    : MemPermit
266  * ABSTRACT  : Acquire access privilege to non-managed memory block of invoking process
267  * NOTE      : To the data of the unmanaged memory block of invoking process
268  *           : Allocate to accessible area of invoking process
269  * ARGUMENT  : u_int32    mem_adr    Start address of the data area to which access privileges are granted
270  *           : u_int32    size    Size of the data area to be granted access privileges
271  *           : int32    perm    PERM_NONCACHE(Cache invalidation), PERM_CACHE(Cache enabled)
272  *           : void**    ptr        Returns the start address of the actual data area that can be accessed
273  * RETURN    : RET_API    RET_NORMAL    Normal completion
274  *           :             RET_ERROR    ABENDs
275  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
276 RET_API MemPermit(u_int32 mem_adr, u_int32 size, int32 perm, void** ptr) {  // LCOV_EXCL_START 8:dead code
277     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
278     return RET_NORMAL;
279 }
280 // LCOV_EXCL_STOP
281
282 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
283  * MODULE    : MemProtect
284  * ABSTRACT  : Revoking access privileges to invoking process unmanaged memory blocks
285  * NOTE      : Free unmanaged memory block allocated to invoking process area
286  * ARGUMENT  : u_int32    mem_adr        Start address of the data area from which the access privilege is to be revoked
287  *           : u_int32    size        Size of the data area for which access privileges are to be revoked
288  * RETURN    : RET_API    RET_NORMAL    Access privilege revocation successful
289  *           :             RET_ERROR    Failed to revoke access privilege
290  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
291 RET_API MemProtect(u_int32 mem_adr, u_int32 size) {  // LCOV_EXCL_START 8:dead code
292     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
293     return RET_NORMAL;
294 }
295 // LCOV_EXCL_STOP
296
297 /**
298  * @brief
299  *   Create Share Data
300  *
301  * @param[in]    char*    area_name    Pointer to shared data area name string
302  * @param[in]    u_int32 size        Size of the data portion
303  * @param[out] void**    mem_ptr    Pointer to the start address of the shared data area
304  *
305  * @return RET_API
306  *              RET_NORMAL    Normal
307  *              RET_ERROR        Abnormality
308  */
309 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
310 RET_API _pb_CreateShareData(char* area_name, u_int32 size, void** mem_ptr)   // NOLINT(readability/nolint) WPF_SYSAPI.h
311 #else
312 RET_API _pb_CreateShareData(TCHAR* area_name, u_int32 size, void** mem_ptr)   // NOLINT(readability/nolint) WPF_SYSAPI.h
313 #endif  // _CWORD64_API_DOES_NOT_USE_UNICODE
314 {
315     RET_API            ret_api = RET_NORMAL;
316     TCHAR           *p_area_name = NULL;
317     TCHAR            name[MAX_AREA_NAME_LEN] = {0};
318     HANDLE            h_shm = NULL;
319     _CWORD64_SHMHDR       *p_hdr = reinterpret_cast<_CWORD64_SHMHDR *>(MAP_FAILED);
320     MEMMAP_ITEM       *p_item = NULL;
321 #ifdef UNDER_CE
322     TCHAR            unicode_area_name[MAX_AREA_NAME_LEN] = {0};
323 #endif  // UNDER_CE
324
325     if (mem_ptr == NULL) {
326         ret_api = RET_ERROR;        /* Parameter error */
327     } else if (area_name == NULL) {
328         ret_api = RET_ERROR;        /* Parameter error */
329     } else {
330 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
331 #ifdef UNDER_CE
332         if (area_name[0] == '\0') {
333             ret_api = RET_ERROR;
334         } else {
335             /* Shared memory name character limit processing */
336             if (strlen(area_name) >= MAX_AREA_NAME_LEN) {
337                 ret_api = RET_ERROR;
338             } else {
339                 mbstowcs(unicode_area_name, area_name, MAX_AREA_NAME_LEN);
340                 p_area_name = unicode_area_name;
341             }
342         }
343 #else
344         p_area_name = area_name;
345         if (_tcslen(p_area_name) >= MAX_AREA_NAME_LEN) {
346             ret_api = RET_ERROR;
347         }
348 #endif
349 #else
350         p_area_name = area_name;
351         if (_tcslen(p_area_name) >= MAX_AREA_NAME_LEN) {
352             ret_api = RET_ERROR;
353         }
354 #endif
355     }
356
357     if (ret_api == RET_NORMAL) {
358         if ((p_area_name[0] == __TEXT('\0')) || (size == 0)) {
359             ret_api = RET_ERROR;    /* Parameter error */
360         }
361     }
362
363     if (ret_api == RET_NORMAL) {
364         _tcscpy(name, p_area_name);
365
366         /* Lock Mutex */
367         PbMutexLock(g_p_mem_map_list->h_mutex, INFINITE);  // LCOV_EXCL_BR_LINE 200: no branch
368
369         p_item = FindMemMapItemByName(name);    /* Search memory map object by area name */
370         if (p_item != NULL)              /* When there is already a generated name */ {
371             *mem_ptr = NULL;
372             ret_api = RET_ERROR;    /* Return in error because it has been generated */
373         } else {
374             /* Allocate MEMMAP_ITEM structure from heap */
375             // LCOV_EXCL_BR_START 5: standard lib error
376             p_item = reinterpret_cast<MEMMAP_ITEM *>(PbProcessHeapAlloc(0, sizeof(MEMMAP_ITEM)));
377             // LCOV_EXCL_BR_STOP
378             if (p_item == NULL) {  // LCOV_EXCL_BR_LINE 5: standard lib error
379                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
380                 *mem_ptr = NULL;  // LCOV_EXCL_LINE 5: standard lib error
381                 ret_api = RET_ERROR;    /* Failed to allocate */  // LCOV_EXCL_LINE 5: standard lib error
382             } else {
383                 p_item->h_heap = NULL;
384                 p_item->p_next = NULL;
385                 p_item->p_prev = NULL;
386                 p_item->address = 0;
387                 p_item->h_shared_mem = NULL;
388                 LinkToMemMapList(p_item);    /* Add to end of memory map list */    // LCOV_EXCL_BR_LINE 200: no branch
389
390                 /* Allocate shared memory */
391                 h_shm = CreateSharedMemory(name, size + sizeof(_CWORD64_SHMHDR));  // LCOV_EXCL_BR_LINE 200: can not be null
392                 if (h_shm == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be null
393                     // LCOV_EXCL_START 200: can not be null
394                     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
395                     /* Remove the target memory-mapped object from the memory-mapped list */
396                     UnlinkFromMemMapList(p_item);
397                     PbProcessHeapFree(0, p_item);
398                     p_item = NULL;
399                     *mem_ptr = NULL;
400                     ret_api = RET_ERROR;
401                     // LCOV_EXCL_STOP
402                 } else {
403                     p_item->h_shared_mem = h_shm;
404                      /* Get Accessible Address */
405                     // LCOV_EXCL_BR_START 200: can not be null
406                     p_hdr = reinterpret_cast<_CWORD64_SHMHDR *>(GetSharedMemoryPtr(p_item->h_shared_mem));
407                     // LCOV_EXCL_BR_STOP
408                     if (p_hdr == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be null
409                         // LCOV_EXCL_START 200: can not be null
410                         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
411                         /* Delete allocated shared memory */
412                         CloseSharedMemory(h_shm);
413                         DeleteSharedMemory(name);
414                         /* Remove the target memory-mapped object from the memory-mapped list */
415                         UnlinkFromMemMapList(p_item);
416                         PbProcessHeapFree(0, p_item);
417                         p_item = NULL;
418                         *mem_ptr = NULL;
419                         ret_api = RET_ERROR;
420                         // LCOV_EXCL_STOP
421                     } else {
422                         p_hdr->mem_size = size;
423                         *mem_ptr = reinterpret_cast<void*>(p_hdr + 1);    /* Start at the address following the header */
424                         _tcscpy(p_item->name, name);    /* Name registration in the table */
425                     }
426                 }
427             }
428         }
429
430         /* Release Mutex */
431         PbMutexUnlock(g_p_mem_map_list->h_mutex);  // LCOV_EXCL_BR_LINE 200: no branch
432     } else {
433         /* When an error occurs during parameter check */
434         if (mem_ptr != NULL) {
435             *mem_ptr = NULL;
436         }
437     }
438
439     return ret_api;
440 }
441
442 /**
443  * @brief
444  *   Link Share Data
445  *
446  * @param[in]    char*    area_name    Pointer to shared data area name string
447  * @param[out] void**    mem_ptr    Pointer to the start address of the shared data area
448  * @param[out] u_int32* size        Size of the data portion
449  *
450  * @return RET_API
451  *              RET_NORMAL    Normal
452  *              RET_ERROR        Abnormality
453  */
454 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
455 RET_API _pb_LinkShareData(char* area_name, void** mem_ptr, u_int32* size)   // NOLINT(readability/nolint) WPF_SYSAPI.h
456 #else
457 RET_API _pb_LinkShareData(TCHAR* area_name, void** mem_ptr, u_int32* size)   // NOLINT(readability/nolint) WPF_SYSAPI.h
458 #endif   // _CWORD64_API_DOES_NOT_USE_UNICODE
459 {
460     RET_API            ret_api = RET_NORMAL;
461     TCHAR           *p_area_name = NULL;
462     TCHAR            name[MAX_AREA_NAME_LEN] = {0};
463     _CWORD64_SHMHDR       *p_hdr = reinterpret_cast<_CWORD64_SHMHDR *>(MAP_FAILED);
464     MEMMAP_ITEM       *p_item = NULL;
465     HANDLE            h_shm = NULL;
466     HANDLE            h_shm_temp = NULL;
467     DWORD            mem_size = 0;
468 #ifdef UNDER_CE
469     TCHAR            unicode_area_name[MAX_AREA_NAME_LEN] = {0};
470 #endif
471
472     if (mem_ptr == NULL) {
473         ret_api = RET_ERROR;        /* Parameter error */
474     } else if ((area_name == NULL) || (size == NULL)) {
475         ret_api = RET_ERROR;        /* Parameter error */
476     } else {
477 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
478 #ifdef UNDER_CE
479         if (area_name[0] == '\0') {
480             ret_api = RET_ERROR;
481         } else {
482             /* Shared memory name character limit processing */
483             if (strlen(area_name) >= MAX_AREA_NAME_LEN) {
484                 ret_api = RET_ERROR;
485             } else {
486                 mbstowcs(unicode_area_name, area_name, MAX_AREA_NAME_LEN);
487                 p_area_name = unicode_area_name;
488             }
489         }
490 #else
491         p_area_name = area_name;
492         if (_tcslen(p_area_name) >= MAX_AREA_NAME_LEN) {
493             ret_api = RET_ERROR;
494         }
495 #endif
496 #else
497         p_area_name = area_name;
498         if (_tcslen(p_area_name) >= MAX_AREA_NAME_LEN) {
499             ret_api = RET_ERROR;
500         }
501 #endif
502     }
503
504     if (ret_api == RET_NORMAL) {
505         if (p_area_name[0] == __TEXT('\0')) {
506             ret_api = RET_ERROR;    /* Parameter error */
507         }
508     }
509
510     if (ret_api == RET_NORMAL) {
511         *size = 0;
512         _tcscpy(name, p_area_name);
513         mem_size = (u_int32) * size;
514
515         /* Lock Mutex */
516         PbMutexLock(g_p_mem_map_list->h_mutex, INFINITE);  // LCOV_EXCL_BR_LINE 200: no branch
517
518         p_item = FindMemMapItemByName(name);            /* Search memory map object by area name */
519         if (p_item != NULL) {
520             /* When there is already a generated name */
521             h_shm = p_item->h_shared_mem;
522             // LCOV_EXCL_BR_START 200: can not be null
523             p_hdr = reinterpret_cast<_CWORD64_SHMHDR*>(GetSharedMemoryPtr(h_shm));    /* Get Accessible Address */
524             // LCOV_EXCL_BR_STOP
525             if (p_hdr == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be null
526                 // LCOV_EXCL_START 200: can not be null
527                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
528                 *mem_ptr = NULL;
529                 ret_api = RET_ERROR;
530                 // LCOV_EXCL_STOP
531             } else {
532                 /* Start at the address following the header */
533                 *mem_ptr = reinterpret_cast<void *>(p_hdr + 1);
534                 *size = static_cast<u_int32>(p_hdr->mem_size);
535             }
536         } else {
537             /* When no memory map object has been created */
538             /* Allocate MEMMAP_ITEM structure from heap */
539             // LCOV_EXCL_BR_START 5: standard lib error
540             p_item = reinterpret_cast<MEMMAP_ITEM*>(PbProcessHeapAlloc(0, sizeof(MEMMAP_ITEM)));
541             // LCOV_EXCL_BR_STOP
542             if (p_item == NULL) {  // LCOV_EXCL_BR_LINE 5: standard lib error
543                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
544                 *mem_ptr = NULL;  // LCOV_EXCL_LINE 5: standard lib error
545                 ret_api = RET_ERROR;    /* Failed to allocate */  // LCOV_EXCL_LINE 5: standard lib error
546             } else {
547                 p_item->h_heap = NULL;
548                 p_item->p_next = NULL;
549                 p_item->p_prev = NULL;
550                 p_item->address = 0;
551                 _tcscpy(p_item->name, name);
552                 p_item->h_shared_mem = NULL;
553                 LinkToMemMapList(p_item);    /* Add to end of memory map list */  // LCOV_EXCL_BR_LINE 200: no branch
554
555                 ret_api = RET_ERROR;        /* Process result initialization */
556                 /* Maps the shared data area to physical memory and returns its information pointer
557                  * (Check if not found by table lookup, but other processes are already reserved) */
558                 h_shm_temp = OpenSharedMemory(p_item->name, sizeof(_CWORD64_SHMHDR));  // LCOV_EXCL_BR_LINE 200: no branch
559                 if (h_shm_temp != NULL) {
560                     /* Get Accessible Address */
561                     p_hdr = reinterpret_cast<_CWORD64_SHMHDR*>(GetSharedMemoryPtr(h_shm_temp));  // LCOV_EXCL_BR_LINE 200: can not be NULL  // NOLINT(whitespace/line_length)
562                     if (p_hdr != NULL) {  // LCOV_EXCL_BR_LINE 200: can not be NULL
563                         mem_size = p_hdr->mem_size;
564                         ret_api = RET_NORMAL;
565                     }
566                     CloseSharedMemory(h_shm_temp);  // LCOV_EXCL_BR_LINE 200: no branch
567                 }
568
569                 if (ret_api == RET_NORMAL) {
570                     ret_api = RET_ERROR;    /* Process result initialization */
571                     /* Maps the shared data area to physical memory and returns its information pointer */
572                     h_shm = OpenSharedMemory(p_item->name, mem_size);  // LCOV_EXCL_BR_LINE 200: no branch
573                     if (h_shm != NULL) {
574                         p_item->h_shared_mem = h_shm;
575                         /* Get Accessible Address */
576                         p_hdr = reinterpret_cast<_CWORD64_SHMHDR*>(GetSharedMemoryPtr(p_item->h_shared_mem));
577                         if (p_hdr != NULL) {
578                             *mem_ptr = reinterpret_cast<void*>(p_hdr + 1);    /* Start at the address following the header */
579                             *size = static_cast<u_int32>(mem_size);
580                             ret_api = RET_NORMAL;
581                         }
582                     }
583                 }
584
585                 if (ret_api != RET_NORMAL) {
586                     UnlinkFromMemMapList(p_item);    /* Remove the target memory-mapped object from the memory-mapped list */
587
588                     PbProcessHeapFree(0, p_item);  // LCOV_EXCL_BR_LINE 200: no branch
589                     p_item = NULL;
590                     *mem_ptr = NULL;
591                     *size = 0;
592                 }
593             }
594         }
595
596         /* Release Mutex */
597         PbMutexUnlock(g_p_mem_map_list->h_mutex);  // LCOV_EXCL_BR_LINE 200: no branch
598     } else {
599         /* When an error occurs during parameter check */
600         if (mem_ptr != NULL) {
601             *mem_ptr = NULL;
602         }
603     }
604
605     return ret_api;
606 }
607
608 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
609  * MODULE    : PbDeleteShareData
610  * ABSTRACT  : Shared data area deletion processing
611  * NOTE      : Deletes (releases) the shared memory area allocated under the name of the shared data area.
612  * ARGUMENT  : char*        area_name        Pointer to shared data area name string
613  * RETURN    : RET_API        RET_NORMAL        Normal
614  *           :                RET_ERROR        The specified shared data does not exist
615  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
616 RET_API
617 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
618 PbDeleteShareData(char* area_name)  // LCOV_EXCL_START 8:dead code
619 #else
620 PbDeleteShareData(TCHAR* area_name)
621 #endif   // _CWORD64_API_DOES_NOT_USE_UNICODE
622 {
623     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
624     RET_API            ret_api = RET_NORMAL;
625     TCHAR           *p_area_name = NULL;
626     TCHAR            name[MAX_AREA_NAME_LEN] = {0};
627     MEMMAP_ITEM       *p_item = NULL;
628 #ifdef UNDER_CE
629     TCHAR            unicode_area_name[MAX_AREA_NAME_LEN] = {0};
630 #endif
631
632     /* Parameter check */
633     if (area_name == NULL)          /* If the name of the shared area is NULL */ {
634         ret_api = RET_ERROR;
635     } else {
636 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
637 #ifdef UNDER_CE
638         if (strlen(area_name) >= MAX_AREA_NAME_LEN) {
639             ret_api = RET_ERROR;
640         } else {
641             mbstowcs(unicode_area_name, area_name, MAX_AREA_NAME_LEN);
642             p_area_name = unicode_area_name;
643         }
644 #else
645         p_area_name = area_name;
646         if (_tcslen(p_area_name) >= MAX_AREA_NAME_LEN) {
647             ret_api = RET_ERROR;
648         }
649 #endif
650 #else
651         p_area_name = area_name;
652         if (_tcslen(p_area_name) >= MAX_AREA_NAME_LEN) {
653             ret_api = RET_ERROR;
654         }
655 #endif
656         if (ret_api == RET_NORMAL) {
657             if (p_area_name[0] == __TEXT('\0')) {
658                 ret_api = RET_ERROR;
659             }
660         }
661     }
662
663     if (ret_api == RET_NORMAL) {
664         /* Shared memory is allocated with the name of the shared area, or the memory map information management table is searched. */
665         _tcscpy(name, p_area_name);
666
667         /* Lock Mutex */
668         PbMutexLock(g_p_mem_map_list->h_mutex, INFINITE);
669
670         p_item = FindMemMapItemByName(name);        /* Search memory map object by area name */
671         if (p_item == NULL)                          /* If the area name is not in the memory-map information management TBL */ {
672             /* If the area name is not in the memory map information management TBL, it has not been created or deleted. */
673             /* Release Mutex */
674             PbMutexUnlock(g_p_mem_map_list->h_mutex);
675             ret_api = RET_ERROR;
676         } else {
677             /* If the area name is in the memory map information management TBL */
678             /* Removes the memory map information from the Chain of the memory map information management table. */
679             UnlinkFromMemMapList(p_item);
680
681             /* Releases the Mutex of the memory map information management table and releases the shared memory. */
682             /* Release Mutex */
683             PbMutexUnlock(g_p_mem_map_list->h_mutex);
684
685             if (p_item->h_shared_mem != NULL) {
686                 CloseSharedMemory(p_item->h_shared_mem);
687                 p_item->h_shared_mem = NULL;
688             }
689
690             /* Frees memory-map information from the heap */
691             PbProcessHeapFree(0, p_item);
692
693             /* Deleting Shared Memory Objects */
694             /*************************************************************/
695             /* Note: Be sure to delete it, so Do not use DeleteShareData */
696             /* if you want to link elsewhere.                            */
697             /*************************************************************/
698             DeleteSharedMemory(name);
699         }
700     }
701
702     return ret_api;
703 }
704 // LCOV_EXCL_STOP
705
706 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
707  * MODULE    : LinkRomData
708  * ABSTRACT  : ROM data link
709  * NOTE      : Links data located in ROM.
710  *           : Specifies the name of the file in which the data is to be stored, and points to the start address of the data.
711  *           : Get pointer and data size.
712  * ARGUMENT  : char        *filename    Pointer to the string of the name of the ROM data storage file
713  *           : void        **mem_ptr    Pointer to the start address of the ROM data storage file
714  *           : u_int32    *size        Pointer to the size of the data portion
715  * RETURN    : RET_API    RET_NORMAL    Normal status
716  *           :             RET_ERROR    Specified ROM data storage file does not exist
717  *           :                         File name character string width error
718  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
719 #ifdef _CWORD64_API_DOES_NOT_USE_UNICODE
720 RET_API LinkRomData(char* filename, void** mem_ptr, u_int32* size)  // LCOV_EXCL_START 8:dead code
721 #else
722 RET_API LinkRomData(TCHAR* filename, void** mem_ptr, u_int32* size)
723 #endif   // _CWORD64_API_DOES_NOT_USE_UNICODE
724 {
725     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
726     return RET_NORMAL;    /* Coverity CID: 18772 compliant */
727 }
728 // LCOV_EXCL_STOP
729
730 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
731  * MODULE    : PbAccessPhysicalMem
732  * ABSTRACT  : Access to Physical Memory Area Allocation Data
733  * NOTE      : Access data allocated in the physical memory area.
734  *           : The physical memory area is mapped to the shared memory area and mapped.
735  *           : The start address is returned.
736  * ARGUMENT  : u_int32    addr        Start address of the physical memory area
737  *           : void        **mem_ptr    Pointer to the start address of the mapped shared area
738  *           : u_int32    size        Size of the data
739  *           : u_int32    mode        Access mode
740  *           :                             ACCESS_MODE_READONLY :Read Only
741  *           :                             ACCESS_MODE_READWRITE:Reading and Writing
742  * RETURN    : RET_API    RET_NORMAL        Normal status
743  *           :             RET_ERRPARAM    Parameter error
744  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
745 RET_API
746 PbAccessPhysicalMem(u_int32 addr, void **mem_ptr, u_int32 size, u_int32 mode) {  // LCOV_EXCL_START 8:dead code
747     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
748     return RET_NORMAL;    /* Coverity CID: 18767 compliant */
749 }
750 // LCOV_EXCL_STOP
751
752 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
753  * MODULE    : PbFreePhysicalMem
754  * ABSTRACT  : Access release processing to the physical memory area allocation data
755  * NOTE      : Releases access to data allocated in a physical memory area
756  *           : Releases the shared memory area Allocate by AccessPhysicalMem.
757  * ARGUMENT  : u_int32    addr        Start address of the physical memory area to be released
758  *           : void        *mem_ptr    Pointer to the start address of the mapped shared area
759  *           : u_int32    size        Data size to be released
760  * RETURN    : RET_API    RET_NORMAL        Normal status
761  *           :             RET_ERRPARAM    Parameter error
762  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
763 RET_API
764 PbFreePhysicalMem(u_int32 addr, void *mem_ptr, u_int32 size) {  // LCOV_EXCL_START 8:dead code
765     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
766     return RET_NORMAL;    /* Coverity CID: 18766 compliant */
767 }
768 // LCOV_EXCL_STOP
769
770 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
771  * MODULE    : FindMemMapItemByName
772  * ABSTRACT  : Memory map information retrieval processing
773  * NOTE      : Retrieves the memory map information by the specified shared data area name.
774  * ARGUMENT  : TCHAR*            name        Pointer to shared data area name character string
775  * RETURN    : MEMMAP_ITEM*        !NULL        Pointer to memory map information
776  *           :                    NULL        Memory map information does not exist
777  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
778 static MEMMAP_ITEM*
779 FindMemMapItemByName(TCHAR* name) {
780     MEMMAP_ITEM* ret = NULL;    /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
781     if (g_p_mem_map_list == NULL) {  // LCOV_EXCL_BR_LINE 200: g_p_mem_map_list can not be NULL
782         /* If the memory map information management table is not allocated, */
783         /* nop */
784         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
785     } else if (g_p_mem_map_list->num_of_items == 0) {
786         /* If no memory map information is allocated, */
787         /* nop */
788         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
789     } else {
790         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
791         MEMMAP_ITEM* p_item = g_p_mem_map_list->p_head;
792         /* Gets the pointer of the memory map Chain at the beginning of the file. */
793         /* Loops until there is no memory map Chain */
794         while (p_item != NULL) {
795             if (_tcscmp(p_item->name, name) == 0) {
796                 /* If the name of the memory map information matches the name of the argument */
797                 /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
798                 /* Returns a pointer to the memory map information */
799                 ret = p_item;
800                 break;    /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
801             }
802             p_item = p_item->p_next;                    /* Retrieves the pointers of the memory map data Chain next time. */
803         }
804     }
805
806     return(ret);   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
807 }
808
809 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
810  * MODULE    : LinkToMemMapList
811  * NOTE      : Adding a Memory Map Object to the Memory Map List
812  * ARGUMENT  : MEMMAP_ITEM*    p_item    Specify a memory-mapped object
813  * RETURN    : Without
814  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
815 static void
816 LinkToMemMapList(MEMMAP_ITEM* p_item) {
817     BOOL    processing_complete = FALSE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
818     if (g_p_mem_map_list == NULL) {  // LCOV_EXCL_BR_LINE 200: g_p_mem_map_list can not be NULL
819         // LCOV_EXCL_START 200: g_p_mem_map_list can not be NULL
820         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
821         processing_complete = TRUE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
822         // LCOV_EXCL_STOP
823     }
824
825     /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
826     if ((processing_complete == FALSE) && (g_p_mem_map_list->num_of_items == 0)) {
827         g_p_mem_map_list->p_head = p_item;
828         g_p_mem_map_list->p_tail = p_item;
829         g_p_mem_map_list->num_of_items = 1;
830         processing_complete = TRUE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
831     }
832
833     /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
834     if (processing_complete == FALSE) {
835         g_p_mem_map_list->p_tail->p_next = p_item;
836         p_item->p_prev = g_p_mem_map_list->p_tail;
837         p_item->p_next = NULL;
838         g_p_mem_map_list->p_tail = p_item;
839         g_p_mem_map_list->num_of_items++;
840     }
841     return;
842 }
843
844 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
845  * MODULE    : UnlinkFromMemMapList
846  * ABSTRACT  : Memory map information deletion processing
847  * NOTE      : Memory map information specified by the pointer is stored in the memory map information management table.
848  *           : From the Chain structures.The memory map information is not released.
849  * ARGUMENT  : MEMMAP_ITEM*        p_item        Pointer to the memory map information to be deleted
850  * RETURN    : None
851  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
852 static void
853 UnlinkFromMemMapList(MEMMAP_ITEM* p_item) {
854     /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
855     BOOL    processing_complete = FALSE;
856     /* Parameter check */
857     if (g_p_mem_map_list == NULL) {  // LCOV_EXCL_BR_LINE 200: g_p_mem_map_list can not be NULL
858         /* If the memory map information management table is not allocated, */
859         /* nop */
860         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
861     } else if (p_item == NULL) {  // LCOV_EXCL_BR_LINE 200: p_item can not be NULL
862         /* If a pointer to memory-mapped information is not specified, */
863         /* nop */
864         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
865     } else if (g_p_mem_map_list->num_of_items == 0) {  // LCOV_EXCL_BR_LINE 200: num_of_items can not be 0
866         /* If no memory map information is allocated, */
867         /* nop */
868         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
869     } else {
870         /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
871         /* Chain detachment process when there is only one memory map data item */
872         if (g_p_mem_map_list->num_of_items == 1) {
873             if (g_p_mem_map_list->p_head == p_item) {
874                 /* If only one memory map is reserved */
875                 /* For the top memory map information of the memo map information management table */
876                 g_p_mem_map_list->p_head = NULL;            /* Initializes the top memory map to NULL. */
877                 g_p_mem_map_list->p_tail = NULL;            /* NULL initialization of the termination memory map data */
878                 g_p_mem_map_list->num_of_items = 0;        /* Initializes the control memory map information count to zero. */
879             }
880             /* Specified memory map information pointer does not exist  */
881             processing_complete = TRUE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
882         }
883
884         /* Memory-map-information-removal process at the top of the Chain */
885         if ((processing_complete == FALSE) && (p_item == g_p_mem_map_list->p_head))  {
886             /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
887             /* If the specified memory map information is the first memory map information, */
888             /* The leading memory map information pointer is changed to the next Chain pointer. */
889             g_p_mem_map_list->p_head = g_p_mem_map_list->p_head->p_next;
890             /* Change the previous Chain source of the first memory map to NULL */
891             g_p_mem_map_list->p_head->p_prev = NULL;
892             g_p_mem_map_list->num_of_items--;        /* Decrement the number of management memory map information */
893             p_item->p_next = NULL;         /* Initialize the Chain destination of the removed memory map NULL. */
894             processing_complete = TRUE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
895         }
896
897         /* Memory-map data detachment process at the end of the Chain */
898         if ((processing_complete == FALSE) && (p_item == g_p_mem_map_list->p_tail)) {
899             /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
900             /* If the pointed-to memory-map information is terminated, */
901             /* Change the terminating memory-map info pointer to the previous Chain source */
902             g_p_mem_map_list->p_tail = g_p_mem_map_list->p_tail->p_prev;
903             /* Change the Chain destination of the terminated memory map to NULL. */
904             g_p_mem_map_list->p_tail->p_next = NULL;
905             /* Decrement the number of management memory map information */
906             g_p_mem_map_list->num_of_items--;
907             /* The previous Chain source of the removed memory map data is NULL initialized. */
908             p_item->p_prev = NULL;
909             processing_complete = TRUE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
910         }
911
912         /* Checking the memory map info Chain for errors */
913         if ((processing_complete == FALSE) &&
914                 (g_p_mem_map_list->num_of_items <= 2)) {
915             /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
916             /* No more than two memory maps are possible except at the beginning and end. */
917             processing_complete = TRUE;   /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
918         }
919
920         /* Departure process other than the start and end of the memory map data Chain */
921         if (processing_complete == FALSE) {
922             /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
923             p_item->p_prev->p_next = p_item->p_next;     /* Set the next Chain destination of the previous memory map to the next one */
924             p_item->p_next->p_prev = p_item->p_prev;     /* Previous Chain of one memory map before previous */
925             g_p_mem_map_list->num_of_items--;             /* Decrement the number of management memory map information */
926             p_item->p_prev = NULL;                 /* The previous Chain source of the removed memory map data is NULL initialized. */
927             p_item->p_next = NULL;                 /* Initialize the Chain destination of the removed memory map NULL. */
928         }
929     }
930     return;
931 }
932
933 /**
934  * @brief
935  *   Obtain dump information
936  *
937  * @param[out] p_buf      Dump info
938  */
939 void _pb_GetDebugMemoryMngTbl(void* p_buf) {    // NOLINT(readability/nolint) WPF_SYSAPI.h
940     static uint8_t  buf[DEBUG_DUMP_MAX_SIZE];
941     static uint8_t  buf_tmp[256];
942     uint32_t        i = 0;
943
944     if (p_buf != NULL) {
945         memset(&buf[0], 0x00, sizeof(buf));
946         snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf), "Memory");
947
948         if (g_p_mem_map_list == NULL) {  // LCOV_EXCL_BR_LINE 200: g_p_mem_map_list can not be NULL
949             // LCOV_EXCL_START 200: g_p_mem_map_list can not be NULL
950             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
951             strncat(reinterpret_cast<char *>(&buf[0]), "\n NULL", strlen("\n NULL"));
952             // LCOV_EXCL_STOP
953         } else if (g_p_mem_map_list->num_of_items == 0) {  // LCOV_EXCL_BR_LINE 200: num_of_items can not be 0
954             // LCOV_EXCL_START 200: num_of_items can not be 0
955             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
956             strncat(reinterpret_cast<char *>(&buf[0]), "\n num_of_items:0", strlen("\n num_of_items:0"));
957             // LCOV_EXCL_STOP
958         } else {
959             memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
960             snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),  // LCOV_EXCL_BR_LINE 5: c lib error case
961                     "\n h_heap:%p, p_head:%s, p_tail:%s, num_of_items:%lu, h_mutex:%p",
962                     g_p_mem_map_list->h_heap,
963                     ((g_p_mem_map_list->p_head == NULL)?"NULL":"NOT NULL"),
964                     ((g_p_mem_map_list->p_tail == NULL)?"NULL":"NOT NULL"),
965                     g_p_mem_map_list->num_of_items,
966                     g_p_mem_map_list->h_mutex);
967             strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
968                 strlen(reinterpret_cast<char *>(&buf_tmp[0])));
969             MEMMAP_ITEM* p_item = g_p_mem_map_list->p_head;
970             while (p_item != NULL) {
971                 memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
972                 snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
973                         "\n [%02d]h_heap:%10p, p_next:%s, p_prev:%s, name:%40s, hShrMem:%10p, adr:%lu",
974                         i,
975                         p_item->h_heap,
976                         ((p_item->p_next == NULL)?"NULL    ":"NOT NULL"),
977                         ((p_item->p_prev == NULL)?"NULL    ":"NOT NULL"),
978                         p_item->name,
979                         p_item->h_shared_mem,
980                         p_item->address);
981                 i++;
982                 strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
983                     strlen(reinterpret_cast<char *>(&buf_tmp[0])));
984                 p_item = p_item->p_next;
985             }
986         }
987         memcpy(p_buf, &buf[0], sizeof(buf));
988     }
989 }