Import upstream u-boot 1.1.4
[u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / ntdrv / stdlib.c
1 /****************************************************************************
2 *
3 *                   SciTech OS Portability Manager Library
4 *
5 *  ========================================================================
6 *
7 *    The contents of this file are subject to the SciTech MGL Public
8 *    License Version 1.0 (the "License"); you may not use this file
9 *    except in compliance with the License. You may obtain a copy of
10 *    the License at http://www.scitechsoft.com/mgl-license.txt
11 *
12 *    Software distributed under the License is distributed on an
13 *    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 *    implied. See the License for the specific language governing
15 *    rights and limitations under the License.
16 *
17 *    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 *    The Initial Developer of the Original Code is SciTech Software, Inc.
20 *    All Rights Reserved.
21 *
22 *  ========================================================================
23 *
24 * Language:     ANSI C
25 * Environment:  32-bit Windows NT driver
26 *
27 * Description:  C library compatible stdlib.h functions for use within a
28 *               Windows NT driver.
29 *
30 ****************************************************************************/
31
32 #include "pmapi.h"
33 #include "oshdr.h"
34
35 /*------------------------ Main Code Implementation -----------------------*/
36
37 /****************************************************************************
38 REMARKS:
39 PM_malloc override function for Nucleus drivers loaded in NT drivers's.
40 ****************************************************************************/
41 void * malloc(
42     size_t size)
43 {
44     return PM_mallocShared(size);
45 }
46
47 /****************************************************************************
48 REMARKS:
49 calloc library function for Nucleus drivers loaded in NT drivers's.
50 ****************************************************************************/
51 void * calloc(
52     size_t nelem,
53     size_t size)
54 {
55     void *p = PM_mallocShared(nelem * size);
56     if (p)
57         memset(p,0,nelem * size);
58     return p;
59 }
60
61 /****************************************************************************
62 REMARKS:
63 PM_realloc override function for Nucleus drivers loaded in VxD's.
64 ****************************************************************************/
65 void * realloc(
66     void *ptr,
67     size_t size)
68 {
69     void *p = PM_mallocShared(size);
70     if (p) {
71         memcpy(p,ptr,size);
72         PM_freeShared(ptr);
73         }
74     return p;
75 }
76
77 /****************************************************************************
78 REMARKS:
79 PM_free override function for Nucleus drivers loaded in VxD's.
80 ****************************************************************************/
81 void free(
82     void *p)
83 {
84     PM_freeShared(p);
85 }
86
87 /****************************************************************************
88 PARAMETERS:
89 cstr    - C style ANSI string to convert
90
91 RETURNS:
92 Pointer to the UniCode string structure or NULL on failure to allocate memory
93
94 REMARKS:
95 Converts a C style string to a UniCode string structure that can be passed
96 directly to NT kernel functions.
97 ****************************************************************************/
98 UNICODE_STRING *_PM_CStringToUnicodeString(
99     const char *cstr)
100 {
101     int             length;
102     ANSI_STRING     ansiStr;
103     UNICODE_STRING  *uniStr;
104
105     /* Allocate memory for the string structure */
106     if ((uniStr = ExAllocatePool(NonPagedPool, sizeof(UNICODE_STRING))) == NULL)
107         return NULL;
108
109     /* Allocate memory for the wide string itself */
110     length = (strlen(cstr) * sizeof(WCHAR)) + sizeof(WCHAR);
111     if ((uniStr->Buffer = ExAllocatePool(NonPagedPool, length)) == NULL) {
112         ExFreePool(uniStr);
113         return NULL;
114         }
115     RtlZeroMemory(uniStr->Buffer, length);
116     uniStr->Length = 0;
117     uniStr->MaximumLength = (USHORT)length;
118
119     /* Convert filename string to ansi string and then to UniCode string */
120     RtlInitAnsiString(&ansiStr, cstr);
121     RtlAnsiStringToUnicodeString(uniStr, &ansiStr, FALSE);
122     return uniStr;
123 }
124
125 /****************************************************************************
126 PARAMETERS:
127 uniStr  - UniCode string structure to free
128
129 REMARKS:
130 Frees a string allocated by the above _PM_CStringToUnicodeString function.
131 ****************************************************************************/
132 void _PM_FreeUnicodeString(
133     UNICODE_STRING *uniStr)
134 {
135     if (uniStr) {
136         ExFreePool(uniStr->Buffer);
137         ExFreePool(uniStr);
138         }
139 }