Home | History | Annotate | Download | only in EfiDriverLib
      1 /*++
      2 
      3 Copyright (c) 2004 - 2007, Intel Corporation. All rights reserved.<BR>
      4 This program and the accompanying materials
      5 are licensed and made available under the terms and conditions of the BSD License
      6 which accompanies this distribution.  The full text of the license may be found at
      7 http://opensource.org/licenses/bsd-license.php
      8 
      9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     11 
     12 Module Name:
     13 
     14   EfiLibAllocate.c
     15 
     16 Abstract:
     17 
     18   Support routines for memory allocation routines for use with drivers.
     19 
     20 --*/
     21 
     22 #include "Tiano.h"
     23 #include "EfiDriverLib.h"
     24 
     25 VOID *
     26 EfiLibAllocatePool (
     27   IN  UINTN   AllocationSize
     28   )
     29 /*++
     30 
     31 Routine Description:
     32 
     33   Allocate BootServicesData pool.
     34 
     35 Arguments:
     36 
     37   AllocationSize  - The size to allocate
     38 
     39 Returns:
     40 
     41   Pointer of the buffer allocated.
     42 
     43 --*/
     44 {
     45   VOID  *Memory;
     46 
     47   Memory = NULL;
     48   gBS->AllocatePool (EfiBootServicesData, AllocationSize, &Memory);
     49   return Memory;
     50 }
     51 
     52 VOID *
     53 EfiLibAllocateRuntimePool (
     54   IN  UINTN   AllocationSize
     55   )
     56 /*++
     57 
     58 Routine Description:
     59 
     60   Allocate RuntimeServicesData pool.
     61 
     62 Arguments:
     63 
     64   AllocationSize  - The size to allocate
     65 
     66 Returns:
     67 
     68   Pointer of the buffer allocated.
     69 
     70 --*/
     71 {
     72   VOID  *Memory;
     73 
     74   Memory = NULL;
     75   gBS->AllocatePool (EfiRuntimeServicesData, AllocationSize, &Memory);
     76   return Memory;
     77 }
     78 
     79 VOID *
     80 EfiLibAllocateZeroPool (
     81   IN  UINTN   AllocationSize
     82   )
     83 /*++
     84 
     85 Routine Description:
     86 
     87   Allocate BootServicesData pool and zero it.
     88 
     89 Arguments:
     90 
     91   AllocationSize  - The size to allocate
     92 
     93 Returns:
     94 
     95   Pointer of the buffer allocated.
     96 
     97 --*/
     98 {
     99   VOID  *Memory;
    100 
    101   Memory = EfiLibAllocatePool (AllocationSize);
    102   if (Memory != NULL) {
    103     gBS->SetMem (Memory, AllocationSize, 0);
    104   }
    105 
    106   return Memory;
    107 }
    108 
    109 VOID *
    110 EfiLibAllocateRuntimeZeroPool (
    111   IN  UINTN   AllocationSize
    112   )
    113 /*++
    114 
    115 Routine Description:
    116 
    117   Allocate RuntimeServicesData pool and zero it.
    118 
    119 Arguments:
    120 
    121   AllocationSize  - The size to allocate
    122 
    123 Returns:
    124 
    125   Pointer of the buffer allocated.
    126 
    127 --*/
    128 {
    129   VOID  *Memory;
    130 
    131   Memory = EfiLibAllocateRuntimePool (AllocationSize);
    132   if (Memory != NULL) {
    133     gBS->SetMem (Memory, AllocationSize, 0);
    134   }
    135 
    136   return Memory;
    137 }
    138 
    139 VOID *
    140 EfiLibAllocateCopyPool (
    141   IN  UINTN   AllocationSize,
    142   IN  VOID    *Buffer
    143   )
    144 /*++
    145 
    146 Routine Description:
    147 
    148   Allocate BootServicesData pool and use a buffer provided by
    149   caller to fill it.
    150 
    151 Arguments:
    152 
    153   AllocationSize  - The size to allocate
    154 
    155   Buffer          - Buffer that will be filled into the buffer allocated
    156 
    157 Returns:
    158 
    159   Pointer of the buffer allocated.
    160 
    161 --*/
    162 {
    163   VOID  *Memory;
    164 
    165   Memory = NULL;
    166   gBS->AllocatePool (EfiBootServicesData, AllocationSize, &Memory);
    167   if (Memory != NULL) {
    168     gBS->CopyMem (Memory, Buffer, AllocationSize);
    169   }
    170 
    171   return Memory;
    172 }
    173 
    174 VOID *
    175 EfiLibAllocateRuntimeCopyPool (
    176   IN  UINTN            AllocationSize,
    177   IN  VOID             *Buffer
    178   )
    179 /*++
    180 
    181 Routine Description:
    182 
    183   Allocate RuntimeServicesData pool and use a buffer provided by
    184   caller to fill it.
    185 
    186 Arguments:
    187 
    188   AllocationSize  - The size to allocate
    189 
    190   Buffer          - Buffer that will be filled into the buffer allocated
    191 
    192 Returns:
    193 
    194   Pointer of the buffer allocated.
    195 
    196 --*/
    197 {
    198   VOID  *Memory;
    199 
    200   Memory = NULL;
    201   gBS->AllocatePool (EfiRuntimeServicesData, AllocationSize, &Memory);
    202   if (Memory != NULL) {
    203     gBS->CopyMem (Memory, Buffer, AllocationSize);
    204   }
    205 
    206   return Memory;
    207 }
    208 
    209 
    210 VOID
    211 EfiLibSafeFreePool (
    212   IN  VOID             *Buffer
    213   )
    214 /*++
    215 
    216 Routine Description:
    217 
    218   Free pool safely (without setting back Buffer to NULL).
    219 
    220 Arguments:
    221 
    222   Buffer          - The allocated pool entry to free
    223 
    224 Returns:
    225 
    226   Pointer of the buffer allocated.
    227 
    228 --*/
    229 {
    230   if (Buffer != NULL) {
    231     gBS->FreePool (Buffer);
    232   }
    233 }
    234