summaryrefslogtreecommitdiffstats
path: root/private/unimodem/inc/rovmem.h
blob: 26b429c8e2e1dd6fe595720b40370e26cc7d3efa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// Copyright (c) Microsoft Corporation 1993-1995
//
// rovmem.h
//
// Memory management functions.
//
// History:
//  09-27-94 ScottH     Partially taken from commctrl
//  04-29-95 ScottH     Taken from briefcase and cleaned up
//

// This file is included by <rovcomm.h>

#ifndef _ROVMEM_H_
#define _ROVMEM_H_

//
// Memory routines
//

//
// Non-shared memory allocation
//

//      void * GAlloc(DWORD cbBytes)
//          Alloc a chunk of memory, quickly, with no 64k limit on size of
//          individual objects or total object size.  Initialize to zero.
//
#define GAlloc(cbBytes)         GlobalAlloc(GPTR, cbBytes)

//      void * GReAlloc(void * pv, DWORD cbNewSize)
//          Realloc one of above.  If pv is NULL, then this function will do
//          an alloc for you.  Initializes new portion to zero.
//
#define GReAlloc(pv, cbNewSize) GlobalReAlloc(pv, cbNewSize, GMEM_MOVEABLE | GMEM_ZEROINIT)

//      void GFree(void *pv)
//          Free pv if it is nonzero.  Set pv to zero.  
//
#define GFree(pv)        do { (pv) ? GlobalFree(pv) : (void)0;  pv = NULL; } while (0)

//      DWORD GGetSize(void *pv)
//          Get the size of a block allocated by Alloc()
//
#define GGetSize(pv)            GlobalSize(pv)

//      type * GAllocType(type);                    (macro)
//          Alloc some memory the size of <type> and return pointer to <type>.
//
#define GAllocType(type)                (type FAR *)GAlloc(sizeof(type))

//      type * GAllocArray(type, int cNum);         (macro)
//          Alloc an array of data the size of <type>.
//
#define GAllocArray(type, cNum)          (type FAR *)GAlloc(sizeof(type) * (cNum))

//      type * GReAllocArray(type, void * pb, int cNum);
//
#define GReAllocArray(type, pb, cNum)    (type FAR *)GReAlloc(pb, sizeof(type) * (cNum))

//      (Re)allocates *ppszBuf and copies psz into *ppszBuf 
//
BOOL    
PUBLIC 
SetStringW(
    LPWSTR FAR * ppwszBuf, 
    LPCWSTR pwsz);
BOOL    
PUBLIC 
SetStringA(
    LPSTR FAR * ppszBuf, 
    LPCSTR psz);
#ifdef UNICODE
#define SetString   SetStringW
#else  // UNICODE
#define SetString   SetStringA
#endif // UNICODE


//      (Re)allocates *ppszBuf and concatenates psz onto *ppszBuf 
//
BOOL 
PUBLIC 
CatStringW(
    IN OUT LPWSTR FAR * ppszBuf,
    IN     LPCWSTR     psz);
BOOL 
PUBLIC 
CatStringA(
    IN OUT LPSTR FAR * ppszBuf,
    IN     LPCSTR      psz);
#ifdef UNICODE
#define CatString   CatStringW
#else  // UNICODE
#define CatString   CatStringA
#endif // UNICODE


BOOL 
PUBLIC 
CatMultiStringW(
    IN OUT LPWSTR FAR * ppszBuf,
    IN     LPCWSTR     psz);
BOOL 
PUBLIC 
CatMultiStringA(
    IN OUT LPSTR FAR * ppszBuf,
    IN     LPCSTR      psz);
#ifdef UNICODE
#define CatMultiString      CatMultiStringW
#else  // UNICODE
#define CatMultiString      CatMultiStringA
#endif // UNICODE


//
// Shared memory allocation functions.
//
#if !defined(NOSHAREDHEAP) && defined(WIN95)

//      SharedTerminate() must be called before the app/dll is terminated.
//
void    PUBLIC SharedTerminate();

//      PVOID SharedAlloc(DWORD cb);
//          Alloc a chunk of memory, quickly, with no 64k limit on size of
//          individual objects or total object size.  Initialize to zero.
//
PVOID   PUBLIC SharedAlloc(DWORD cb);                              

//      PVOID SharedReAlloc(PVOID pv, DWORD cb);
//          Realloc one of above.  If pb is NULL, then this function will do
//          an alloc for you.  Initializes new portion to zero.
//
PVOID   PUBLIC SharedReAlloc(PVOID pv, DWORD cb);

//      void SharedFree(PVOID pv);
//          If pv is nonzero, free it.  Sets pv to zero.
//
void    PUBLIC _SharedFree(PVOID pv);
#define SharedFree(pv)        do { (pv) ? _SharedFree(pv) : (void)0;  pv = NULL; } while (0)

//      DWORD SharedGetSize(PVOID pv);
//          Get the size of a block allocated by Alloc()
//      
DWORD   PUBLIC SharedGetSize(PVOID pv);                      


//      type * SharedAllocType(type);                    (macro)
//          Alloc some memory the size of <type> and return pointer to <type>.
//
#define SharedAllocType(type)           (type FAR *)SharedAlloc(sizeof(type))

//      type * SharedAllocArray(type, int cNum);         (macro)
//          Alloc an array of data the size of <type>.
//
#define SharedAllocArray(type, cNum)    (type FAR *)SharedAlloc(sizeof(type) * (cNum))

//      type * SharedReAllocArray(type, void * pb, int cNum);
//
#define SharedReAllocArray(type, pb, cNum) (type FAR *)SharedReAlloc(pb, sizeof(type) * (cNum))

//      (Re)allocates *ppszBuf and copies psz into *ppszBuf 
//
BOOL    PUBLIC SharedSetString(LPTSTR FAR * ppszBuf, LPCTSTR psz);

#else  // NOSHAREDHEAP

#define SharedAlloc(cbBytes)            GAlloc(cbBytes)
#define SharedReAlloc(pv, cb)           GReAlloc(pv, cb)
#define SharedFree(pv)                  GFree(pv)
#define SharedGetSize(pv)               GGetSize(pv)
#define SharedAllocType(type)           (type FAR *)SharedAlloc(sizeof(type))
#define SharedAllocArray(type, cNum)    (type FAR *)SharedAlloc(sizeof(type) * (cNum))
#define SharedReAllocArray(type, pb, cNum) (type FAR *)SharedReAlloc(pb, sizeof(type) * (cNum))
#define SharedSetString(ppszBuf, psz)   SetString(ppszBuf, psz)

#endif // NOSHAREDHEAP


#ifndef NODA
//
// Internal functions 
//

#ifdef WIN32
//
// These macros are used in our controls, that in 32 bits we simply call
// LocalAlloc as to have the memory associated with the process that created
// it and as such will be cleaned up if the process goes away.
//

LPVOID  PUBLIC MemAlloc(HANDLE hheap, DWORD cb);
LPVOID  PUBLIC MemReAlloc(HANDLE hheap, LPVOID pb, DWORD cb);
BOOL    PUBLIC MemFree(HANDLE hheap, LPVOID pb);
DWORD   PUBLIC MemSize(HANDLE hheap, LPVOID pb);

#else // WIN32

// In 16 bit code we need the Allocs to go from our heap code as we do not
// want to limit them to 64K of data.  If we have some type of notification of
// 16 bit application termination, We may want to see if we can
// dedicate different heaps for different processes to cleanup...

#define MemAlloc(hheap, cb)       Alloc(cb)  /* calls to verify heap exists */
#define MemReAlloc(hheap, pb, cb) ReAlloc(pb, cb)
#define MemFree(hheap, pb)        Free(pb)
#define MemSize(hheap, pb)        GetSize((LPCVOID)pb)

#endif // WIN32


//
// Structure Array
//
#define SA_ERR      (-1)
#define SA_APPEND   0x7fffffff

typedef struct _SA FAR * HSA;                                            
                                                                          
HSA     PUBLIC SACreate(int cbItem, int cItemGrow, HANDLE hheap, DWORD dwFlags);

// Flags for SACreate
#define SAF_DEFAULT     0x0000
#define SAF_SHARED      0x0001
#define SAF_HEAP        0x0002

BOOL    PUBLIC SADestroy(HSA hsa);                                
BOOL    PUBLIC SAGetItem(HSA hsa, int i, LPVOID pitem);        
LPVOID  PUBLIC SAGetItemPtr(HSA hsa, int i);                      
BOOL    PUBLIC SASetItem(HSA hsa, int i, LPVOID pitem);        
int     PUBLIC SAInsertItem(HSA hsa, int i, LPVOID pitem);     
BOOL    PUBLIC SADeleteItem(HSA hsa, int i);                      
BOOL    PUBLIC SADeleteAllItems(HSA hsa);                         
#define        SAGetCount(hsa)          (*(int FAR*)(hsa))             
    
//                                                                      
// Pointer Array
//
#define PA_ERR      (-1)
#define PA_APPEND   0x7fffffff

typedef struct _PA FAR * HPA;                                            
                                                                          
HPA     PUBLIC PACreate(int cItemGrow, HANDLE hheap, DWORD dwFlags);

// Flags for PACreate
#define PAF_DEFAULT     0x0000
#define PAF_SHARED      0x0001
#define PAF_HEAP        0x0002

typedef void (CALLBACK *PFNPAFREE)(LPVOID pv, LPARAM lParam);

BOOL    PUBLIC PADestroyEx(HPA hpa, PFNPAFREE pfnFree, LPARAM lParam);
#define        PADestroy(hpa)           PADestroyEx(hpa, NULL, 0)

HPA     PUBLIC PAClone(HPA hpa, HPA hpaNew);                    
LPVOID  PUBLIC PAGetPtr(HPA hpa, int i);                          
int     PUBLIC PAGetPtrIndex(HPA hpa, LPVOID p);               
BOOL    PUBLIC PAGrow(HPA pdpa, int cp);                           
BOOL    PUBLIC PASetPtr(HPA hpa, int i, LPVOID p);             
int     PUBLIC PAInsertPtr(HPA hpa, int i, LPVOID p);          
LPVOID  PUBLIC PADeletePtr(HPA hpa, int i);
BOOL    PUBLIC PADeleteAllPtrsEx(HPA hpa, PFNPAFREE pfnFree, LPARAM lParam);
#define        PADeleteAllPtrs(hpa)     PADeleteAllPtrsEx(hpa, NULL, 0)
#define        PAGetCount(hpa)          (*(int FAR*)(hpa))
#define        PAGetPtrPtr(hpa)         (*((LPVOID FAR* FAR*)((BYTE FAR*)(hpa) + sizeof(int))))
#define        PAFastGetPtr(hpa, i)     (PAGetPtrPtr(hpa)[i])  

typedef int (CALLBACK *PFNPACOMPARE)(LPVOID p1, LPVOID p2, LPARAM lParam);
                                                                          
BOOL   PUBLIC PASort(HPA hpa, PFNPACOMPARE pfnCompare, LPARAM lParam);
                                                                          
// Search array.  If PAS_SORTED, then array is assumed to be sorted      
// according to pfnCompare, and binary search algorithm is used.          
// Otherwise, linear search is used.                                      
//                                                                        
// Searching starts at iStart (0 to start search at beginning).          
//                                                                        
// PAS_INSERTBEFORE/AFTER govern what happens if an exact match is not   
// found.  If neither are specified, this function returns -1 if no exact 
// match is found.  Otherwise, the index of the item before or after the  
// closest (including exact) match is returned.                           
//                                                                        
// Search option flags                                                    
//                                                                        
#define PAS_SORTED             0x0001                                	  
#define PAS_INSERTBEFORE       0x0002                                    
#define PAS_INSERTAFTER        0x0004                                    
                                                                          
int PUBLIC PASearch(HPA hpa, LPVOID pFind, int iStart,
              PFNPACOMPARE pfnCompare,
              LPARAM lParam, UINT options);
#endif // NODA

#endif // _ROVMEM_H_