1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** POSIX wrapper for THREADX                                             */
16 /**                                                                       */
17 /**                                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /* Include necessary system files.  */
23 
24 #include "tx_api.h"    /* Threadx API */
25 #include "pthread.h"  /* Posix API */
26 #include "px_int.h"    /* Posix helper functions */
27 
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    posix_memory_allocate                               PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function attempts to obtain the specified amount of memory     */
42 /*    from the POSIX heap.                                                */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    size                                  Number of bytes in the pool   */
47 /*    memory_ptr                            Pointer to memory ptr variable*/
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    ERROR                                 If error occurs               */
52 /*    retval                                Return value                  */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    tx_byte_allocate                      Allocate from the pool        */
57 /*    posix_internal_error                  Generic posix error           */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    POSIX internal code                                                 */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
68 /*                                                                        */
69 /**************************************************************************/
posix_memory_allocate(ULONG size,VOID ** memory_ptr)70 INT posix_memory_allocate(ULONG size, VOID **memory_ptr)
71 {
72 
73 INT    retval;
74 
75     /* Init in case we fail.  */
76     *memory_ptr = (VOID *)0;
77 
78     /* Force all alignments to long word boundaries to be safe.  */
79     if (size & 0x03)
80     {
81         /* Bump size up to next 4 byte boundary.  */
82         size = ((size + 0x03) & ~0x03);
83     }
84     /* Attempt to allocate the desired memory from the POSIX heap.
85        Do not wait - if memory isn't available, flag an error.  */
86     retval = tx_byte_allocate((TX_BYTE_POOL *)&posix_heap_byte_pool, memory_ptr,
87                               size, TX_NO_WAIT);
88 
89     /* Make sure the memory was obtained successfully.  */
90     if (retval)
91     {
92         /* Error obtaining memory.  */
93         posix_internal_error(555);
94         /* return error.  */
95         return(ERROR);
96     }
97     /* Return to caller.  */
98     return(retval);
99 }
100