]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c
CryptoPkg/CrtLibSupport: add secure_getenv() stub function
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / BaseMemAllocation.c
1 /** @file
2 Base Memory Allocation Routines Wrapper for Crypto library over OpenSSL
3 during PEI & DXE phases.
4
5 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <CrtLibSupport.h>
17 #include <Library/MemoryAllocationLib.h>
18
19 //
20 // Extra header to record the memory buffer size from malloc routine.
21 //
22 #define CRYPTMEM_HEAD_SIGNATURE SIGNATURE_32('c','m','h','d')
23 typedef struct {
24 UINT32 Signature;
25 UINT32 Reserved;
26 UINTN Size;
27 } CRYPTMEM_HEAD;
28
29 #define CRYPTMEM_OVERHEAD sizeof(CRYPTMEM_HEAD)
30
31 //
32 // -- Memory-Allocation Routines --
33 //
34
35 /* Allocates memory blocks */
36 void *malloc (size_t size)
37 {
38 CRYPTMEM_HEAD *PoolHdr;
39 UINTN NewSize;
40 VOID *Data;
41
42 //
43 // Adjust the size by the buffer header overhead
44 //
45 NewSize = (UINTN)(size) + CRYPTMEM_OVERHEAD;
46
47 Data = AllocatePool (NewSize);
48 if (Data != NULL) {
49 PoolHdr = (CRYPTMEM_HEAD *)Data;
50 //
51 // Record the memory brief information
52 //
53 PoolHdr->Signature = CRYPTMEM_HEAD_SIGNATURE;
54 PoolHdr->Size = size;
55
56 return (VOID *)(PoolHdr + 1);
57 } else {
58 //
59 // The buffer allocation failed.
60 //
61 return NULL;
62 }
63 }
64
65 /* Reallocate memory blocks */
66 void *realloc (void *ptr, size_t size)
67 {
68 CRYPTMEM_HEAD *OldPoolHdr;
69 CRYPTMEM_HEAD *NewPoolHdr;
70 UINTN OldSize;
71 UINTN NewSize;
72 VOID *Data;
73
74 NewSize = (UINTN)size + CRYPTMEM_OVERHEAD;
75 Data = AllocatePool (NewSize);
76 if (Data != NULL) {
77 NewPoolHdr = (CRYPTMEM_HEAD *)Data;
78 NewPoolHdr->Signature = CRYPTMEM_HEAD_SIGNATURE;
79 NewPoolHdr->Size = size;
80 if (ptr != NULL) {
81 //
82 // Retrieve the original size from the buffer header.
83 //
84 OldPoolHdr = (CRYPTMEM_HEAD *)ptr - 1;
85 ASSERT (OldPoolHdr->Signature == CRYPTMEM_HEAD_SIGNATURE);
86 OldSize = OldPoolHdr->Size;
87
88 //
89 // Duplicate the buffer content.
90 //
91 CopyMem ((VOID *)(NewPoolHdr + 1), ptr, MIN (OldSize, size));
92 FreePool ((VOID *)OldPoolHdr);
93 }
94
95 return (VOID *)(NewPoolHdr + 1);
96 } else {
97 //
98 // The buffer allocation failed.
99 //
100 return NULL;
101 }
102 }
103
104 /* De-allocates or frees a memory block */
105 void free (void *ptr)
106 {
107 CRYPTMEM_HEAD *PoolHdr;
108
109 //
110 // In Standard C, free() handles a null pointer argument transparently. This
111 // is not true of FreePool() below, so protect it.
112 //
113 if (ptr != NULL) {
114 PoolHdr = (CRYPTMEM_HEAD *)ptr - 1;
115 ASSERT (PoolHdr->Signature == CRYPTMEM_HEAD_SIGNATURE);
116 FreePool (PoolHdr);
117 }
118 }