Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 1 | /* |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 2 | * Global heap functions |
| 3 | * |
| 4 | * Copyright 1995 Alexandre Julliard |
| 5 | */ |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 6 | |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame^] | 7 | #include <sys/types.h> |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 8 | #include <stdlib.h> |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 9 | #include <string.h> |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 10 | |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 11 | #include "windows.h" |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 12 | #include "global.h" |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 13 | #include "toolhelp.h" |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 14 | #include "selectors.h" |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 15 | #include "dde_mem.h" |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 16 | #include "stackframe.h" |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame^] | 17 | #include "options.h" |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 18 | #include "stddebug.h" |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 19 | #include "debug.h" |
| 20 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 21 | /* Global arena block */ |
| 22 | typedef struct |
| 23 | { |
| 24 | DWORD base; /* Base address */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 25 | DWORD size; /* Size in bytes (0 indicates a free block) */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 26 | HGLOBAL handle; /* Handle for this block */ |
| 27 | HGLOBAL hOwner; /* Owner of this block */ |
| 28 | BYTE lockCount; /* Count of GlobalFix() calls */ |
| 29 | BYTE pageLockCount; /* Count of GlobalPageLock() calls */ |
| 30 | BYTE flags; /* Allocation flags */ |
| 31 | BYTE selCount; /* Number of selectors allocated for this block */ |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 32 | int shmid; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 33 | } GLOBALARENA; |
| 34 | |
| 35 | /* Flags definitions */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 36 | #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 37 | #define GA_DGROUP 0x04 |
| 38 | #define GA_DISCARDABLE 0x08 |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 39 | #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 40 | |
| 41 | /* Arena array */ |
| 42 | static GLOBALARENA *pGlobalArena = NULL; |
| 43 | static int globalArenaSize = 0; |
| 44 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 45 | #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */ |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 46 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 47 | #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT)) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 48 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 49 | /*********************************************************************** |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 50 | * GLOBAL_GetArena |
| 51 | * |
| 52 | * Return the arena for a given selector, growing the arena array if needed. |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 53 | */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 54 | static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount ) |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 55 | { |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 56 | if (((sel >> __AHSHIFT) + selcount) > globalArenaSize) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 57 | { |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 58 | int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 59 | GLOBALARENA *pNewArena = realloc( pGlobalArena, |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 60 | newsize * sizeof(GLOBALARENA) ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 61 | if (!pNewArena) return 0; |
| 62 | pGlobalArena = pNewArena; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 63 | memset( pGlobalArena + globalArenaSize, 0, |
| 64 | (newsize - globalArenaSize) * sizeof(GLOBALARENA) ); |
| 65 | globalArenaSize = newsize; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 66 | } |
| 67 | return pGlobalArena + (sel >> __AHSHIFT); |
| 68 | } |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 69 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 70 | |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 71 | void debug_handles() |
| 72 | { |
| 73 | int printed=0; |
| 74 | int i; |
| 75 | for (i = globalArenaSize-1 ; i>=0 ; i--) { |
| 76 | if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){ |
| 77 | printed=1; |
| 78 | printf("0x%08x, ",pGlobalArena[i].handle); |
| 79 | } |
| 80 | } |
| 81 | if (printed) |
| 82 | printf("\n"); |
| 83 | } |
| 84 | /*********************************************************************** |
| 85 | * GLOBAL_FindArena |
| 86 | * |
| 87 | * Find the arena for a given handle |
| 88 | * (when handle is not serial - e.g. DDE) |
| 89 | */ |
| 90 | static GLOBALARENA *GLOBAL_FindArena( HGLOBAL handle) |
| 91 | { |
| 92 | int i; |
| 93 | for (i = globalArenaSize-1 ; i>=0 ; i--) { |
| 94 | if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == handle) |
| 95 | return ( &pGlobalArena[i] ); |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 101 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 102 | * GLOBAL_CreateBlock |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 103 | * |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 104 | * Create a global heap block for a fixed range of linear memory. |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 105 | */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 106 | HGLOBAL GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size, |
| 107 | HGLOBAL hOwner, BOOL isCode, |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 108 | BOOL is32Bit, BOOL isReadOnly, |
| 109 | SHMDATA *shmdata ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 110 | { |
| 111 | WORD sel, selcount; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 112 | GLOBALARENA *pArena; |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 113 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 114 | /* Allocate the selector(s) */ |
| 115 | |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 116 | sel = SELECTOR_AllocBlock( ptr, size, |
| 117 | isCode ? SEGMENT_CODE : SEGMENT_DATA, |
| 118 | is32Bit, isReadOnly ); |
| 119 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 120 | if (!sel) return 0; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 121 | selcount = (size + 0xffff) / 0x10000; |
| 122 | |
| 123 | if (!(pArena = GLOBAL_GetArena( sel, selcount ))) |
| 124 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 125 | FreeSelector( sel ); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* Fill the arena block */ |
| 130 | |
| 131 | pArena->base = (DWORD)ptr; |
| 132 | pArena->size = GET_SEL_LIMIT(sel) + 1; |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame^] | 133 | if ((flags & GMEM_DDESHARE) && Options.ipc) |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 134 | { |
| 135 | pArena->handle = shmdata->handle; |
| 136 | pArena->shmid = shmdata->shmid; |
| 137 | shmdata->sel = sel; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel; |
| 142 | pArena->shmid = 0; |
| 143 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 144 | pArena->hOwner = hOwner; |
| 145 | pArena->lockCount = 0; |
| 146 | pArena->pageLockCount = 0; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 147 | pArena->flags = flags & GA_MOVEABLE; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 148 | if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 149 | if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 150 | if (!isCode) pArena->flags |= GA_DGROUP; |
| 151 | pArena->selCount = selcount; |
| 152 | if (selcount > 1) /* clear the next arena blocks */ |
| 153 | memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) ); |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 154 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 155 | return pArena->handle; |
| 156 | } |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 157 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 158 | |
| 159 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 160 | * GLOBAL_FreeBlock |
| 161 | * |
| 162 | * Free a block allocated by GLOBAL_CreateBlock, without touching |
| 163 | * the associated linear memory range. |
| 164 | */ |
| 165 | BOOL GLOBAL_FreeBlock( HGLOBAL handle ) |
| 166 | { |
| 167 | WORD sel; |
| 168 | |
| 169 | if (!handle) return TRUE; |
| 170 | sel = GlobalHandleToSel( handle ); |
| 171 | if (FreeSelector( sel )) return FALSE; /* failed */ |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 172 | memset( GET_ARENA_PTR(sel), 0, sizeof(GLOBALARENA) ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 173 | return TRUE; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /*********************************************************************** |
| 178 | * GLOBAL_Alloc |
| 179 | * |
| 180 | * Implementation of GlobalAlloc() |
| 181 | */ |
| 182 | HGLOBAL GLOBAL_Alloc( WORD flags, DWORD size, HGLOBAL hOwner, |
| 183 | BOOL isCode, BOOL is32Bit, BOOL isReadOnly ) |
| 184 | { |
| 185 | void *ptr; |
| 186 | HGLOBAL handle; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 187 | SHMDATA shmdata; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 188 | |
| 189 | dprintf_global( stddeb, "GlobalAlloc: %ld flags=%04x\n", size, flags ); |
| 190 | |
| 191 | /* Fixup the size */ |
| 192 | |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 193 | if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 194 | if (size == 0) size = 0x20; |
| 195 | else size = (size + 0x1f) & ~0x1f; |
| 196 | |
| 197 | /* Allocate the linear memory */ |
| 198 | |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame^] | 199 | if ((flags & GMEM_DDESHARE) && Options.ipc) |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 200 | ptr= DDE_malloc(flags, size, &shmdata); |
| 201 | else |
| 202 | ptr = malloc( size ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 203 | if (!ptr) return 0; |
| 204 | |
| 205 | /* Allocate the selector(s) */ |
| 206 | |
| 207 | handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 208 | isCode, is32Bit, isReadOnly, &shmdata); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 209 | if (!handle) |
| 210 | { |
| 211 | free( ptr ); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | if (flags & GMEM_ZEROINIT) memset( ptr, 0, size ); |
| 216 | return handle; |
| 217 | } |
| 218 | |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 219 | /*********************************************************************** |
| 220 | * DDE_GlobalHandleToSel |
| 221 | */ |
| 222 | |
| 223 | WORD DDE_GlobalHandleToSel( HGLOBAL handle ) |
| 224 | { |
| 225 | GLOBALARENA *pArena; |
| 226 | SEGPTR segptr; |
| 227 | |
| 228 | pArena= GLOBAL_FindArena(handle); |
| 229 | if (pArena) { |
| 230 | int ArenaIdx = pArena - pGlobalArena; |
| 231 | |
| 232 | /* See if synchronized to the shared memory */ |
| 233 | return DDE_SyncHandle(handle, ( ArenaIdx << __AHSHIFT) | 7); |
| 234 | } |
| 235 | |
| 236 | /* attach the block */ |
| 237 | DDE_AttachHandle(handle, &segptr); |
| 238 | |
| 239 | return SELECTOROF( segptr ); |
| 240 | } |
| 241 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 242 | |
| 243 | /*********************************************************************** |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 244 | * GlobalAlloc (KERNEL.15) |
| 245 | */ |
| 246 | HGLOBAL GlobalAlloc( WORD flags, DWORD size ) |
| 247 | { |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 248 | HANDLE owner = GetCurrentPDB(); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 249 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 250 | if (flags & GMEM_DDESHARE) |
| 251 | owner = GetExePtr(owner); /* Make it a module handle */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 252 | return GLOBAL_Alloc( flags, size, owner, FALSE, FALSE, FALSE ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 253 | } |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 254 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 255 | |
| 256 | /*********************************************************************** |
| 257 | * GlobalReAlloc (KERNEL.16) |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 258 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 259 | HGLOBAL GlobalReAlloc( HGLOBAL handle, DWORD size, WORD flags ) |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 260 | { |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 261 | WORD selcount; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 262 | DWORD oldsize; |
| 263 | void *ptr; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 264 | GLOBALARENA *pArena, *pNewArena; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 265 | WORD sel = GlobalHandleToSel( handle ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 266 | |
| 267 | dprintf_global( stddeb, "GlobalReAlloc: %04x %ld flags=%04x\n", |
| 268 | handle, size, flags ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 269 | if (!handle) return 0; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 270 | |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame^] | 271 | if (Options.ipc && (flags & GMEM_DDESHARE || is_dde_handle(handle))) { |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 272 | fprintf(stdnimp, |
| 273 | "GlobalReAlloc: shared memory reallocating unimplemented\n"); |
| 274 | return 0; |
| 275 | } |
| 276 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 277 | pArena = GET_ARENA_PTR( handle ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 278 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 279 | /* Discard the block if requested */ |
| 280 | |
Alexandre Julliard | ded3038 | 1995-07-06 17:18:27 +0000 | [diff] [blame] | 281 | if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY)) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 282 | { |
| 283 | if (!(pArena->flags & GA_MOVEABLE) || |
| 284 | !(pArena->flags & GA_DISCARDABLE) || |
| 285 | (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0; |
| 286 | free( (void *)pArena->base ); |
| 287 | pArena->base = 0; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 288 | /* Note: we rely on the fact that SELECTOR_ReallocBlock won't */ |
| 289 | /* change the selector if we are shrinking the block */ |
| 290 | SELECTOR_ReallocBlock( sel, 0, 1, SEGMENT_DATA, 0, 0 ); |
| 291 | return handle; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 294 | /* Fixup the size */ |
| 295 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 296 | if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0; |
| 297 | if (size == 0) size = 0x20; |
| 298 | else size = (size + 0x1f) & ~0x1f; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 299 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 300 | /* Change the flags */ |
| 301 | |
| 302 | if (flags & GMEM_MODIFY) |
| 303 | { |
| 304 | /* Change the flags, leaving GA_DGROUP alone */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 305 | pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 306 | if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE; |
| 307 | return handle; |
| 308 | } |
| 309 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 310 | /* Reallocate the linear memory */ |
| 311 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 312 | ptr = (void *)pArena->base; |
| 313 | oldsize = pArena->size; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 314 | dprintf_global(stddeb,"oldsize %08lx\n",oldsize); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 315 | if (size == oldsize) return handle; /* Nothing to do */ |
| 316 | |
| 317 | ptr = realloc( ptr, size ); |
| 318 | if (!ptr) |
| 319 | { |
| 320 | FreeSelector( sel ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 321 | memset( pArena, 0, sizeof(GLOBALARENA) ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* Reallocate the selector(s) */ |
| 326 | |
| 327 | sel = SELECTOR_ReallocBlock( sel, ptr, size, SEGMENT_DATA, 0, 0 ); |
| 328 | if (!sel) |
| 329 | { |
| 330 | free( ptr ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 331 | memset( pArena, 0, sizeof(GLOBALARENA) ); |
| 332 | return 0; |
| 333 | } |
| 334 | selcount = (size + 0xffff) / 0x10000; |
| 335 | |
| 336 | if (!(pNewArena = GLOBAL_GetArena( sel, selcount ))) |
| 337 | { |
| 338 | free( ptr ); |
| 339 | FreeSelector( sel ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 343 | /* Fill the new arena block */ |
| 344 | |
| 345 | if (pNewArena != pArena) memcpy( pNewArena, pArena, sizeof(GLOBALARENA) ); |
| 346 | pNewArena->base = (DWORD)ptr; |
| 347 | pNewArena->size = GET_SEL_LIMIT(sel) + 1; |
| 348 | pNewArena->selCount = selcount; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 349 | pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 350 | |
| 351 | if (selcount > 1) /* clear the next arena blocks */ |
| 352 | memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) ); |
| 353 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 354 | if ((oldsize < size) && (flags & GMEM_ZEROINIT)) |
| 355 | memset( (char *)ptr + oldsize, 0, size - oldsize ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 356 | return pNewArena->handle; |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 357 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | /*********************************************************************** |
| 361 | * GlobalFree (KERNEL.17) |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 362 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 363 | HGLOBAL GlobalFree( HGLOBAL handle ) |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 364 | { |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 365 | void *ptr = GlobalLock( handle ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 366 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 367 | dprintf_global( stddeb, "GlobalFree: %04x\n", handle ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 368 | if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */ |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 369 | if (is_dde_handle(handle)) return DDE_GlobalFree(handle); |
| 370 | if (ptr) free( ptr ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 371 | return 0; |
| 372 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 373 | |
| 374 | |
| 375 | /*********************************************************************** |
| 376 | * WIN16_GlobalLock (KERNEL.18) |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 377 | * |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 378 | * This is the GlobalLock() function used by 16-bit code. |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 379 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 380 | SEGPTR WIN16_GlobalLock( HGLOBAL handle ) |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 381 | { |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 382 | dprintf_global( stddeb, "WIN16_GlobalLock(%04x) -> %08lx\n", |
| 383 | handle, MAKELONG( 0, GlobalHandleToSel(handle)) ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 384 | if (!handle) return 0; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 385 | if ( !is_dde_handle(handle) && !GET_ARENA_PTR(handle)->base) |
| 386 | return (SEGPTR)0; |
| 387 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 388 | return (SEGPTR)MAKELONG( 0, GlobalHandleToSel(handle) ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 389 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 390 | |
| 391 | |
| 392 | /*********************************************************************** |
| 393 | * GlobalLock (KERNEL.18) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 394 | * |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 395 | * This is the GlobalLock() function used by 32-bit code. |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 396 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 397 | LPSTR GlobalLock( HGLOBAL handle ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 398 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 399 | if (!handle) return 0; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 400 | if (is_dde_handle(handle)) { |
| 401 | return DDE_AttachHandle(handle, NULL); |
| 402 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 403 | return (LPSTR)GET_ARENA_PTR(handle)->base; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 404 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 405 | |
| 406 | |
| 407 | /*********************************************************************** |
| 408 | * GlobalUnlock (KERNEL.19) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 409 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 410 | BOOL GlobalUnlock( HGLOBAL handle ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 411 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 412 | dprintf_global( stddeb, "GlobalUnlock: %04x\n", handle ); |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 415 | |
| 416 | |
| 417 | /*********************************************************************** |
| 418 | * GlobalSize (KERNEL.20) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 419 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 420 | DWORD GlobalSize( HGLOBAL handle ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 421 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 422 | dprintf_global( stddeb, "GlobalSize: %04x\n", handle ); |
| 423 | if (!handle) return 0; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 424 | return GET_ARENA_PTR(handle)->size; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 425 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 426 | |
| 427 | |
| 428 | /*********************************************************************** |
| 429 | * GlobalHandle (KERNEL.21) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 430 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 431 | DWORD GlobalHandle( WORD sel ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 432 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 433 | dprintf_global( stddeb, "GlobalHandle: %04x\n", sel ); |
| 434 | return MAKELONG( GET_ARENA_PTR(sel)->handle, sel ); |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 435 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | /*********************************************************************** |
| 439 | * GlobalFlags (KERNEL.22) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 440 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 441 | WORD GlobalFlags( HGLOBAL handle ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 442 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 443 | GLOBALARENA *pArena; |
| 444 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 445 | dprintf_global( stddeb, "GlobalFlags: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 446 | pArena = GET_ARENA_PTR(handle); |
| 447 | return pArena->lockCount | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 448 | ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) | |
| 449 | ((pArena->base == 0) ? GMEM_DISCARDED : 0); |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 450 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 451 | |
| 452 | |
| 453 | /*********************************************************************** |
| 454 | * LockSegment (KERNEL.23) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 455 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 456 | HGLOBAL LockSegment( HGLOBAL handle ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 457 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 458 | dprintf_global( stddeb, "LockSegment: %04x\n", handle ); |
| 459 | if (handle == (HGLOBAL)-1) handle = CURRENT_DS; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 460 | GET_ARENA_PTR(handle)->lockCount++; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 461 | return handle; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 462 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 463 | |
| 464 | |
| 465 | /*********************************************************************** |
| 466 | * UnlockSegment (KERNEL.24) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 467 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 468 | void UnlockSegment( HGLOBAL handle ) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 469 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 470 | dprintf_global( stddeb, "UnlockSegment: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 471 | if (handle == (HGLOBAL)-1) handle = CURRENT_DS; |
| 472 | GET_ARENA_PTR(handle)->lockCount--; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 473 | /* FIXME: this ought to return the lock count in CX (go figure...) */ |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 474 | } |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 475 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 476 | |
| 477 | /*********************************************************************** |
| 478 | * GlobalCompact (KERNEL.25) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 479 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 480 | DWORD GlobalCompact( DWORD desired ) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 481 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 482 | return GLOBAL_MAX_ALLOC_SIZE; |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 485 | |
| 486 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 487 | * GlobalFreeAll (KERNEL.26) |
| 488 | */ |
| 489 | void GlobalFreeAll( HANDLE owner ) |
| 490 | { |
| 491 | DWORD i; |
| 492 | GLOBALARENA *pArena; |
| 493 | |
| 494 | pArena = pGlobalArena; |
| 495 | for (i = 0; i < globalArenaSize; i++, pArena++) |
| 496 | { |
| 497 | if ((pArena->size != 0) && (pArena->hOwner == owner)) |
| 498 | GlobalFree( pArena->handle ); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 504 | * GlobalWire (KERNEL.111) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 505 | */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 506 | SEGPTR GlobalWire( HGLOBAL handle ) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 507 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 508 | return WIN16_GlobalLock( handle ); |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 509 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 510 | |
| 511 | |
| 512 | /*********************************************************************** |
| 513 | * GlobalUnWire (KERNEL.112) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 514 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 515 | BOOL GlobalUnWire( HGLOBAL handle ) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 516 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 517 | return GlobalUnlock( handle ); |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 518 | } |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 519 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 520 | |
| 521 | /*********************************************************************** |
| 522 | * GlobalDOSAlloc (KERNEL.184) |
| 523 | */ |
| 524 | DWORD GlobalDOSAlloc( DWORD size ) |
| 525 | { |
| 526 | WORD sel = GlobalAlloc( GMEM_FIXED, size ); |
| 527 | if (!sel) return 0; |
| 528 | return MAKELONG( sel, sel /* this one ought to be a real-mode segment */ ); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | /*********************************************************************** |
| 533 | * GlobalDOSFree (KERNEL.185) |
| 534 | */ |
| 535 | WORD GlobalDOSFree( WORD sel ) |
| 536 | { |
| 537 | return GlobalFree( GlobalHandle(sel) ) ? sel : 0; |
| 538 | } |
| 539 | |
| 540 | |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 541 | /*********************************************************************** |
| 542 | * SetSwapAreaSize (KERNEL.106) |
| 543 | */ |
| 544 | LONG SetSwapAreaSize( WORD size ) |
| 545 | { |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 546 | dprintf_global(stdnimp, "STUB: SetSwapAreaSize(%d)\n", size ); |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 547 | return MAKELONG( size, 0xffff ); |
| 548 | } |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 549 | |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 550 | |
| 551 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 552 | * GlobalLRUOldest (KERNEL.163) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 553 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 554 | HGLOBAL GlobalLRUOldest( HGLOBAL handle ) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 555 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 556 | dprintf_global( stddeb, "GlobalLRUOldest: %04x\n", handle ); |
| 557 | if (handle == (HGLOBAL)-1) handle = CURRENT_DS; |
| 558 | return handle; |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 561 | |
| 562 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 563 | * GlobalLRUNewest (KERNEL.164) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 564 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 565 | HGLOBAL GlobalLRUNewest( HGLOBAL handle ) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 566 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 567 | dprintf_global( stddeb, "GlobalLRUNewest: %04x\n", handle ); |
| 568 | if (handle == (HGLOBAL)-1) handle = CURRENT_DS; |
| 569 | return handle; |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 572 | |
| 573 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 574 | * GetFreeSpace (KERNEL.169) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 575 | */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 576 | DWORD GetFreeSpace( UINT wFlags ) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 577 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 578 | return GLOBAL_MAX_ALLOC_SIZE; |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 579 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 580 | |
| 581 | |
| 582 | /*********************************************************************** |
| 583 | * GlobalPageLock (KERNEL.191) |
| 584 | */ |
| 585 | WORD GlobalPageLock( HGLOBAL handle ) |
| 586 | { |
| 587 | dprintf_global( stddeb, "GlobalPageLock: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 588 | return ++(GET_ARENA_PTR(handle)->pageLockCount); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
| 592 | /*********************************************************************** |
| 593 | * GlobalPageUnlock (KERNEL.192) |
| 594 | */ |
| 595 | WORD GlobalPageUnlock( HGLOBAL handle ) |
| 596 | { |
| 597 | dprintf_global( stddeb, "GlobalPageUnlock: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 598 | return --(GET_ARENA_PTR(handle)->pageLockCount); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
| 602 | /*********************************************************************** |
| 603 | * GlobalFix (KERNEL.197) |
| 604 | */ |
| 605 | void GlobalFix( HGLOBAL handle ) |
| 606 | { |
| 607 | dprintf_global( stddeb, "GlobalFix: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 608 | GET_ARENA_PTR(handle)->lockCount++; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
| 612 | /*********************************************************************** |
| 613 | * GlobalUnfix (KERNEL.198) |
| 614 | */ |
| 615 | void GlobalUnfix( HGLOBAL handle ) |
| 616 | { |
| 617 | dprintf_global( stddeb, "GlobalUnfix: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 618 | GET_ARENA_PTR(handle)->lockCount--; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | /*********************************************************************** |
| 623 | * FarSetOwner (KERNEL.403) |
| 624 | */ |
| 625 | void FarSetOwner( HANDLE handle, WORD hOwner ) |
| 626 | { |
| 627 | GET_ARENA_PTR(handle)->hOwner = hOwner; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /*********************************************************************** |
| 632 | * FarGetOwner (KERNEL.404) |
| 633 | */ |
| 634 | WORD FarGetOwner( HANDLE handle ) |
| 635 | { |
| 636 | return GET_ARENA_PTR(handle)->hOwner; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
| 640 | /*********************************************************************** |
| 641 | * GlobalHandleToSel (TOOLHELP.50) |
| 642 | */ |
| 643 | WORD GlobalHandleToSel( HGLOBAL handle ) |
| 644 | { |
| 645 | dprintf_toolhelp( stddeb, "GlobalHandleToSel: %04x\n", handle ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 646 | if (!handle) return 0; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 647 | if (is_dde_handle(handle)) |
| 648 | return DDE_GlobalHandleToSel(handle); |
| 649 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 650 | if (!(handle & 7)) |
| 651 | { |
| 652 | fprintf( stderr, "Program attempted invalid selector conversion\n" ); |
| 653 | return handle - 1; |
| 654 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 655 | return handle | 7; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 659 | /*********************************************************************** |
| 660 | * GlobalFirst (TOOLHELP.51) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 661 | */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 662 | BOOL GlobalFirst( GLOBALENTRY *pGlobal, WORD wFlags ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 663 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 664 | if (wFlags == GLOBAL_LRU) return FALSE; |
| 665 | pGlobal->dwNext = 0; |
| 666 | return GlobalNext( pGlobal, wFlags ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 667 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 668 | |
| 669 | |
| 670 | /*********************************************************************** |
| 671 | * GlobalNext (TOOLHELP.52) |
| 672 | */ |
| 673 | BOOL GlobalNext( GLOBALENTRY *pGlobal, WORD wFlags) |
| 674 | { |
| 675 | GLOBALARENA *pArena; |
| 676 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 677 | if (pGlobal->dwNext >= globalArenaSize) return FALSE; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 678 | pArena = pGlobalArena + pGlobal->dwNext; |
| 679 | if (wFlags == GLOBAL_FREE) /* only free blocks */ |
| 680 | { |
| 681 | int i; |
| 682 | for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++) |
| 683 | if (pArena->size == 0) break; /* block is free */ |
| 684 | if (i >= globalArenaSize) return FALSE; |
| 685 | pGlobal->dwNext = i; |
| 686 | } |
| 687 | |
| 688 | pGlobal->dwAddress = pArena->base; |
| 689 | pGlobal->dwBlockSize = pArena->size; |
| 690 | pGlobal->hBlock = pArena->handle; |
| 691 | pGlobal->wcLock = pArena->lockCount; |
| 692 | pGlobal->wcPageLock = pArena->pageLockCount; |
| 693 | pGlobal->wFlags = (GetCurrentPDB() == pArena->hOwner); |
| 694 | pGlobal->wHeapPresent = FALSE; |
| 695 | pGlobal->hOwner = pArena->hOwner; |
| 696 | pGlobal->wType = GT_UNKNOWN; |
| 697 | pGlobal->wData = 0; |
| 698 | pGlobal->dwNext++; |
| 699 | return TRUE; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | /*********************************************************************** |
| 704 | * GlobalInfo (TOOLHELP.53) |
| 705 | */ |
| 706 | BOOL GlobalInfo( GLOBALINFO *pInfo ) |
| 707 | { |
| 708 | int i; |
| 709 | GLOBALARENA *pArena; |
| 710 | |
| 711 | pInfo->wcItems = globalArenaSize; |
| 712 | pInfo->wcItemsFree = 0; |
| 713 | pInfo->wcItemsLRU = 0; |
| 714 | for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++) |
| 715 | if (pArena->size == 0) pInfo->wcItemsFree++; |
| 716 | return TRUE; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | /*********************************************************************** |
| 721 | * GlobalEntryHandle (TOOLHELP.54) |
| 722 | */ |
| 723 | BOOL GlobalEntryHandle( GLOBALENTRY *pGlobal, HGLOBAL hItem ) |
| 724 | { |
| 725 | return FALSE; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /*********************************************************************** |
| 730 | * GlobalEntryModule (TOOLHELP.55) |
| 731 | */ |
| 732 | BOOL GlobalEntryModule( GLOBALENTRY *pGlobal, HMODULE hModule, WORD wSeg ) |
| 733 | { |
| 734 | return FALSE; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /*********************************************************************** |
| 739 | * MemManInfo (TOOLHELP.72) |
| 740 | */ |
| 741 | BOOL MemManInfo( MEMMANINFO *pInfo ) |
| 742 | { |
| 743 | return TRUE; |
| 744 | } |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 745 | |
| 746 | /*********************************************************************** |
| 747 | * GlobalAlloc32 |
| 748 | * implements GlobalAlloc (KERNEL32.316) |
| 749 | * LocalAlloc (KERNEL32.372) |
| 750 | */ |
| 751 | void *GlobalAlloc32(int flags,int size) |
| 752 | { |
| 753 | dprintf_global(stddeb,"GlobalAlloc32(%x,%x)\n",flags,size); |
| 754 | return malloc(size); |
| 755 | } |