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 |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 19 | */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 20 | /* 0xffff sometimes seems to mean: CURRENT_DS */ |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 21 | |
François Gouget | 1425941 | 2001-11-06 20:57:11 +0000 | [diff] [blame] | 22 | #include "config.h" |
Francois Gouget | 386cf6e | 2001-10-14 16:25:47 +0000 | [diff] [blame] | 23 | #include "wine/port.h" |
| 24 | |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Marcus Meissner | 6189c19 | 2000-03-04 19:19:15 +0000 | [diff] [blame] | 27 | #include <time.h> |
Alexandre Julliard | 383da68 | 2000-02-10 22:15:21 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 29 | #ifdef HAVE_UNISTD_H |
| 30 | # include <unistd.h> |
| 31 | #endif |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 32 | #include <string.h> |
Bang Jun-Young | 17eb169 | 2002-07-25 23:57:34 +0000 | [diff] [blame] | 33 | #ifdef HAVE_SYS_PARAM_H |
| 34 | #include <sys/param.h> |
| 35 | #endif |
Olivier Houchard | f41ecf3 | 2002-05-07 01:50:02 +0000 | [diff] [blame] | 36 | #ifdef HAVE_SYS_SYSCTL_H |
| 37 | #include <sys/sysctl.h> |
| 38 | #endif |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 39 | |
Marcus Meissner | 04c3e1d | 1999-02-19 10:37:02 +0000 | [diff] [blame] | 40 | #include "wine/winbase16.h" |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 41 | #include "wine/exception.h" |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 42 | #include "global.h" |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 43 | #include "toolhelp.h" |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 44 | #include "selectors.h" |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 45 | #include "miscemu.h" |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 46 | #include "stackframe.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 47 | #include "wine/debug.h" |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 48 | #include "winerror.h" |
Dimitrie O. Paun | 737d4be | 2002-12-12 23:34:01 +0000 | [diff] [blame] | 49 | #include "excpt.h" |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 50 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 51 | WINE_DEFAULT_DEBUG_CHANNEL(global); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 52 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 53 | /* Global arena block */ |
| 54 | typedef struct |
| 55 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 56 | DWORD base; /* Base address (0 if discarded) */ |
| 57 | DWORD size; /* Size in bytes (0 indicates a free block) */ |
| 58 | HGLOBAL16 handle; /* Handle for this block */ |
| 59 | HGLOBAL16 hOwner; /* Owner of this block */ |
| 60 | BYTE lockCount; /* Count of GlobalFix() calls */ |
| 61 | BYTE pageLockCount; /* Count of GlobalPageLock() calls */ |
| 62 | BYTE flags; /* Allocation flags */ |
| 63 | BYTE selCount; /* Number of selectors allocated for this block */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 64 | } GLOBALARENA; |
| 65 | |
| 66 | /* Flags definitions */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 67 | #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 68 | #define GA_DGROUP 0x04 |
| 69 | #define GA_DISCARDABLE 0x08 |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 70 | #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */ |
Andreas Mohr | d23f506 | 2000-10-02 22:16:21 +0000 | [diff] [blame] | 71 | #define GA_DOSMEM 0x20 |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 72 | |
| 73 | /* Arena array */ |
| 74 | static GLOBALARENA *pGlobalArena = NULL; |
| 75 | static int globalArenaSize = 0; |
| 76 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 77 | #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */ |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 78 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 79 | #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 80 | #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT)) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 81 | |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 82 | /* filter for page-fault exceptions */ |
| 83 | /* It is possible for a bogus global pointer to cause a */ |
| 84 | /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */ |
| 85 | |
| 86 | static WINE_EXCEPTION_FILTER(page_fault) |
| 87 | { |
| 88 | switch (GetExceptionCode()) { |
| 89 | case (EXCEPTION_ACCESS_VIOLATION): |
| 90 | case (EXCEPTION_PRIV_INSTRUCTION): |
| 91 | return EXCEPTION_EXECUTE_HANDLER; |
| 92 | default: |
| 93 | return EXCEPTION_CONTINUE_SEARCH; |
| 94 | } |
| 95 | } |
| 96 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 97 | /*********************************************************************** |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 98 | * GLOBAL_GetArena |
| 99 | * |
| 100 | * 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] | 101 | */ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 102 | static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount ) |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 103 | { |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 104 | if (((sel >> __AHSHIFT) + selcount) > globalArenaSize) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 105 | { |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 106 | int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 107 | GLOBALARENA *pNewArena = realloc( pGlobalArena, |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 108 | newsize * sizeof(GLOBALARENA) ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 109 | if (!pNewArena) return 0; |
| 110 | pGlobalArena = pNewArena; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 111 | memset( pGlobalArena + globalArenaSize, 0, |
| 112 | (newsize - globalArenaSize) * sizeof(GLOBALARENA) ); |
| 113 | globalArenaSize = newsize; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 114 | } |
| 115 | return pGlobalArena + (sel >> __AHSHIFT); |
| 116 | } |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 117 | |
Juergen Schmied | ebc2b77 | 1998-11-14 18:59:30 +0000 | [diff] [blame] | 118 | void debug_handles(void) |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 119 | { |
| 120 | int printed=0; |
| 121 | int i; |
| 122 | for (i = globalArenaSize-1 ; i>=0 ; i--) { |
| 123 | if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){ |
| 124 | printed=1; |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 125 | DPRINTF("0x%08x, ",pGlobalArena[i].handle); |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | if (printed) |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 129 | DPRINTF("\n"); |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 130 | } |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 131 | |
| 132 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 133 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 134 | * GLOBAL_CreateBlock |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 135 | * |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 136 | * Create a global heap block for a fixed range of linear memory. |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 137 | */ |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 138 | HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size, |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 139 | HGLOBAL16 hOwner, unsigned char selflags ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 140 | { |
| 141 | WORD sel, selcount; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 142 | GLOBALARENA *pArena; |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 143 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 144 | /* Allocate the selector(s) */ |
| 145 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 146 | sel = SELECTOR_AllocBlock( ptr, size, selflags ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 147 | if (!sel) return 0; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 148 | selcount = (size + 0xffff) / 0x10000; |
| 149 | |
| 150 | if (!(pArena = GLOBAL_GetArena( sel, selcount ))) |
| 151 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 152 | SELECTOR_FreeBlock( sel ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* Fill the arena block */ |
| 157 | |
| 158 | pArena->base = (DWORD)ptr; |
Alexandre Julliard | 8c540c6 | 2000-11-13 04:16:05 +0000 | [diff] [blame] | 159 | pArena->size = GetSelectorLimit16(sel) + 1; |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 160 | pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 161 | pArena->hOwner = hOwner; |
| 162 | pArena->lockCount = 0; |
| 163 | pArena->pageLockCount = 0; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 164 | pArena->flags = flags & GA_MOVEABLE; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 165 | if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 166 | if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 167 | if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 168 | pArena->selCount = selcount; |
| 169 | if (selcount > 1) /* clear the next arena blocks */ |
| 170 | memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) ); |
Alexandre Julliard | dba420a | 1994-02-02 06:48:31 +0000 | [diff] [blame] | 171 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 172 | return pArena->handle; |
| 173 | } |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 174 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 175 | |
| 176 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 177 | * GLOBAL_FreeBlock |
| 178 | * |
| 179 | * Free a block allocated by GLOBAL_CreateBlock, without touching |
| 180 | * the associated linear memory range. |
| 181 | */ |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 182 | BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 183 | { |
| 184 | WORD sel; |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 185 | GLOBALARENA *pArena; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 186 | |
| 187 | if (!handle) return TRUE; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 188 | sel = GlobalHandleToSel16( handle ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 189 | if (!VALID_HANDLE(sel)) |
| 190 | return FALSE; |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 191 | pArena = GET_ARENA_PTR(sel); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 192 | SELECTOR_FreeBlock( sel ); |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 193 | memset( pArena, 0, sizeof(GLOBALARENA) ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 194 | return TRUE; |
| 195 | } |
| 196 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 197 | /*********************************************************************** |
| 198 | * GLOBAL_MoveBlock |
| 199 | */ |
| 200 | BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size ) |
| 201 | { |
| 202 | WORD sel; |
| 203 | GLOBALARENA *pArena; |
| 204 | |
| 205 | if (!handle) return TRUE; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 206 | sel = GlobalHandleToSel16( handle ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 207 | if (!VALID_HANDLE(sel)) |
| 208 | return FALSE; |
| 209 | pArena = GET_ARENA_PTR(sel); |
| 210 | if (pArena->selCount != 1) |
| 211 | return FALSE; |
| 212 | |
| 213 | pArena->base = (DWORD)ptr; |
| 214 | pArena->size = size; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 215 | SELECTOR_ReallocBlock( sel, ptr, size ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 216 | return TRUE; |
| 217 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 218 | |
| 219 | /*********************************************************************** |
| 220 | * GLOBAL_Alloc |
| 221 | * |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 222 | * Implementation of GlobalAlloc16() |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 223 | */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 224 | HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 225 | { |
| 226 | void *ptr; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 227 | HGLOBAL16 handle; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 228 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 229 | TRACE("%ld flags=%04x\n", size, flags ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 230 | |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 231 | /* If size is 0, create a discarded block */ |
| 232 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 233 | if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 234 | |
| 235 | /* Fixup the size */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 236 | |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 237 | if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0; |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 238 | size = (size + 0x1f) & ~0x1f; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 239 | |
Marcus Meissner | e32a317 | 1999-07-11 14:13:56 +0000 | [diff] [blame] | 240 | /* Allocate the linear memory */ |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 241 | ptr = HeapAlloc( GetProcessHeap(), 0, size ); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 242 | /* FIXME: free discardable blocks and try again? */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 243 | if (!ptr) return 0; |
| 244 | |
| 245 | /* Allocate the selector(s) */ |
| 246 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 247 | handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 248 | if (!handle) |
| 249 | { |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 250 | HeapFree( GetProcessHeap(), 0, ptr ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | if (flags & GMEM_ZEROINIT) memset( ptr, 0, size ); |
| 255 | return handle; |
| 256 | } |
| 257 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 258 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 259 | * GlobalAlloc (KERNEL.15) |
| 260 | * GlobalAlloc16 (KERNEL32.24) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 261 | * RETURNS |
| 262 | * Handle: Success |
| 263 | * NULL: Failure |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 264 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 265 | HGLOBAL16 WINAPI GlobalAlloc16( |
| 266 | UINT16 flags, /* [in] Object allocation attributes */ |
| 267 | DWORD size /* [in] Number of bytes to allocate */ |
| 268 | ) { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 269 | HANDLE16 owner = GetCurrentPDB16(); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 270 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 271 | if (flags & GMEM_DDESHARE) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 272 | owner = GetExePtr(owner); /* Make it a module handle */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 273 | return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 274 | } |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 275 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 276 | |
| 277 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 278 | * GlobalReAlloc (KERNEL.16) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 279 | * RETURNS |
| 280 | * Handle: Success |
| 281 | * NULL: Failure |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 282 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 283 | HGLOBAL16 WINAPI GlobalReAlloc16( |
| 284 | HGLOBAL16 handle, /* [in] Handle of global memory object */ |
| 285 | DWORD size, /* [in] New size of block */ |
| 286 | UINT16 flags /* [in] How to reallocate object */ |
| 287 | ) { |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 288 | WORD selcount; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 289 | DWORD oldsize; |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 290 | void *ptr, *newptr; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 291 | GLOBALARENA *pArena, *pNewArena; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 292 | WORD sel = GlobalHandleToSel16( handle ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 293 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 294 | TRACE("%04x %ld flags=%04x\n", |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 295 | handle, size, flags ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 296 | if (!handle) return 0; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 297 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 298 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 299 | WARN("Invalid handle 0x%04x!\n", handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 300 | return 0; |
| 301 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 302 | pArena = GET_ARENA_PTR( handle ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 303 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 304 | /* Discard the block if requested */ |
| 305 | |
Alexandre Julliard | ded3038 | 1995-07-06 17:18:27 +0000 | [diff] [blame] | 306 | if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY)) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 307 | { |
| 308 | if (!(pArena->flags & GA_MOVEABLE) || |
| 309 | !(pArena->flags & GA_DISCARDABLE) || |
| 310 | (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0; |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 311 | HeapFree( GetProcessHeap(), 0, (void *)pArena->base ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 312 | pArena->base = 0; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 313 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 314 | /* Note: we rely on the fact that SELECTOR_ReallocBlock won't |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 315 | * change the selector if we are shrinking the block. |
| 316 | * FIXME: shouldn't we keep selectors until the block is deleted? |
| 317 | */ |
Alexandre Julliard | 284c9b9 | 1999-04-11 15:07:13 +0000 | [diff] [blame] | 318 | SELECTOR_ReallocBlock( sel, 0, 1 ); |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 319 | return handle; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 322 | /* Fixup the size */ |
| 323 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 324 | if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0; |
| 325 | if (size == 0) size = 0x20; |
| 326 | else size = (size + 0x1f) & ~0x1f; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 327 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 328 | /* Change the flags */ |
| 329 | |
| 330 | if (flags & GMEM_MODIFY) |
| 331 | { |
| 332 | /* Change the flags, leaving GA_DGROUP alone */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 333 | pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 334 | if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE; |
| 335 | return handle; |
| 336 | } |
| 337 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 338 | /* Reallocate the linear memory */ |
| 339 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 340 | ptr = (void *)pArena->base; |
| 341 | oldsize = pArena->size; |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 342 | TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size); |
Alexandre Julliard | 7d654eb | 1996-02-25 11:36:22 +0000 | [diff] [blame] | 343 | if (ptr && (size == oldsize)) return handle; /* Nothing to do */ |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 344 | |
Andreas Mohr | d23f506 | 2000-10-02 22:16:21 +0000 | [diff] [blame] | 345 | if (pArena->flags & GA_DOSMEM) |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 346 | newptr = DOSMEM_ResizeBlock(ptr, size, NULL); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 347 | else |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 348 | /* if more then one reader (e.g. some pointer has been given out by GetVDMPointer32W16), |
| 349 | only try to realloc in place */ |
| 350 | newptr = HeapReAlloc( GetProcessHeap(), |
| 351 | (pArena->pageLockCount > 0)?HEAP_REALLOC_IN_PLACE_ONLY:0, ptr, size ); |
| 352 | if (!newptr) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 353 | { |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 354 | FIXME("Realloc failed lock %d\n",pArena->pageLockCount); |
| 355 | if (pArena->pageLockCount <1) |
| 356 | { |
| 357 | HeapFree( GetProcessHeap(), 0, ptr ); |
| 358 | SELECTOR_FreeBlock( sel ); |
| 359 | memset( pArena, 0, sizeof(GLOBALARENA) ); |
| 360 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 361 | return 0; |
| 362 | } |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 363 | ptr = newptr; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 364 | |
| 365 | /* Reallocate the selector(s) */ |
| 366 | |
Alexandre Julliard | 284c9b9 | 1999-04-11 15:07:13 +0000 | [diff] [blame] | 367 | sel = SELECTOR_ReallocBlock( sel, ptr, size ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 368 | if (!sel) |
| 369 | { |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 370 | HeapFree( GetProcessHeap(), 0, ptr ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 371 | memset( pArena, 0, sizeof(GLOBALARENA) ); |
| 372 | return 0; |
| 373 | } |
| 374 | selcount = (size + 0xffff) / 0x10000; |
| 375 | |
| 376 | if (!(pNewArena = GLOBAL_GetArena( sel, selcount ))) |
| 377 | { |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 378 | HeapFree( GetProcessHeap(), 0, ptr ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 379 | SELECTOR_FreeBlock( sel ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 383 | /* Fill the new arena block |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 384 | As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/ |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 385 | |
Uwe Bonnes | 9788815 | 2001-12-20 00:00:25 +0000 | [diff] [blame] | 386 | if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 387 | pNewArena->base = (DWORD)ptr; |
Alexandre Julliard | 8c540c6 | 2000-11-13 04:16:05 +0000 | [diff] [blame] | 388 | pNewArena->size = GetSelectorLimit16(sel) + 1; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 389 | pNewArena->selCount = selcount; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 390 | pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 391 | |
| 392 | if (selcount > 1) /* clear the next arena blocks */ |
| 393 | memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) ); |
| 394 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 395 | if ((oldsize < size) && (flags & GMEM_ZEROINIT)) |
| 396 | memset( (char *)ptr + oldsize, 0, size - oldsize ); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 397 | return pNewArena->handle; |
Alexandre Julliard | 7cbe657 | 1995-01-09 18:21:16 +0000 | [diff] [blame] | 398 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 399 | |
| 400 | |
| 401 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 402 | * GlobalFree (KERNEL.17) |
| 403 | * GlobalFree16 (KERNEL32.31) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 404 | * RETURNS |
| 405 | * NULL: Success |
| 406 | * Handle: Failure |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 407 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 408 | HGLOBAL16 WINAPI GlobalFree16( |
| 409 | HGLOBAL16 handle /* [in] Handle of global memory object */ |
| 410 | ) { |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 411 | void *ptr; |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 412 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 413 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 414 | WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | ptr = (void *)GET_ARENA_PTR(handle)->base; |
| 418 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 419 | TRACE("%04x\n", handle ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 420 | if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */ |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 421 | if (ptr) HeapFree( GetProcessHeap(), 0, ptr ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 422 | return 0; |
| 423 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 424 | |
| 425 | |
| 426 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 427 | * GlobalLock (KERNEL.18) |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 428 | * |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 429 | * This is the GlobalLock16() function used by 16-bit code. |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 430 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 431 | SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle ) |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 432 | { |
Ulrich Weigand | b2682d4 | 1999-07-27 16:10:24 +0000 | [diff] [blame] | 433 | WORD sel = GlobalHandleToSel16( handle ); |
| 434 | TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) ); |
| 435 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 436 | if (handle) |
| 437 | { |
| 438 | if (handle == (HGLOBAL16)-1) handle = CURRENT_DS; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 439 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 440 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 441 | WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle); |
Ulrich Weigand | b2682d4 | 1999-07-27 16:10:24 +0000 | [diff] [blame] | 442 | sel = 0; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 443 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 444 | else if (!GET_ARENA_PTR(handle)->base) |
Ulrich Weigand | b2682d4 | 1999-07-27 16:10:24 +0000 | [diff] [blame] | 445 | sel = 0; |
| 446 | else |
| 447 | GET_ARENA_PTR(handle)->lockCount++; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 448 | } |
Ulrich Weigand | b2682d4 | 1999-07-27 16:10:24 +0000 | [diff] [blame] | 449 | |
| 450 | CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */ |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 451 | return MAKESEGPTR( sel, 0 ); |
Alexandre Julliard | 121bd98 | 1993-07-08 17:37:25 +0000 | [diff] [blame] | 452 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 453 | |
| 454 | |
Alexandre Julliard | 5801723 | 2000-12-22 01:09:26 +0000 | [diff] [blame] | 455 | /********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 456 | * K32WOWGlobalLock16 (KERNEL32.60) |
Alexandre Julliard | 5801723 | 2000-12-22 01:09:26 +0000 | [diff] [blame] | 457 | */ |
| 458 | SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem ) |
| 459 | { |
| 460 | return WIN16_GlobalLock16( hMem ); |
| 461 | } |
| 462 | |
| 463 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 464 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 465 | * GlobalLock16 (KERNEL32.25) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 466 | * |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 467 | * This is the GlobalLock16() function used by 32-bit code. |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 468 | * |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 469 | * RETURNS |
| 470 | * Pointer to first byte of memory block |
| 471 | * NULL: Failure |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 472 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 473 | LPVOID WINAPI GlobalLock16( |
| 474 | HGLOBAL16 handle /* [in] Handle of global memory object */ |
| 475 | ) { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 476 | if (!handle) return 0; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 477 | if (!VALID_HANDLE(handle)) |
| 478 | return (LPVOID)0; |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 479 | GET_ARENA_PTR(handle)->lockCount++; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 480 | return (LPVOID)GET_ARENA_PTR(handle)->base; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 481 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 482 | |
| 483 | |
| 484 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 485 | * GlobalUnlock (KERNEL.19) |
| 486 | * GlobalUnlock16 (KERNEL32.26) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 487 | * NOTES |
| 488 | * Should the return values be cast to booleans? |
| 489 | * |
| 490 | * RETURNS |
| 491 | * TRUE: Object is still locked |
| 492 | * FALSE: Object is unlocked |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 493 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 494 | BOOL16 WINAPI GlobalUnlock16( |
| 495 | HGLOBAL16 handle /* [in] Handle of global memory object */ |
| 496 | ) { |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 497 | GLOBALARENA *pArena = GET_ARENA_PTR(handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 498 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 499 | WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 502 | TRACE("%04x\n", handle ); |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 503 | if (pArena->lockCount) pArena->lockCount--; |
| 504 | return pArena->lockCount; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 505 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 506 | |
Ulrich Weigand | 23e9b04 | 1998-12-01 15:19:54 +0000 | [diff] [blame] | 507 | /*********************************************************************** |
| 508 | * GlobalChangeLockCount (KERNEL.365) |
| 509 | * |
| 510 | * This is declared as a register function as it has to preserve |
| 511 | * *all* registers, even AX/DX ! |
| 512 | * |
| 513 | */ |
Ulrich Weigand | b5151d0 | 1999-07-25 11:27:36 +0000 | [diff] [blame] | 514 | void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta, |
| 515 | CONTEXT86 *context ) |
Ulrich Weigand | 23e9b04 | 1998-12-01 15:19:54 +0000 | [diff] [blame] | 516 | { |
Ulrich Weigand | 23e9b04 | 1998-12-01 15:19:54 +0000 | [diff] [blame] | 517 | if ( delta == 1 ) |
| 518 | GlobalLock16( handle ); |
| 519 | else if ( delta == -1 ) |
| 520 | GlobalUnlock16( handle ); |
| 521 | else |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 522 | ERR("(%04X, %d): strange delta value\n", handle, delta ); |
Ulrich Weigand | 23e9b04 | 1998-12-01 15:19:54 +0000 | [diff] [blame] | 523 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 524 | |
| 525 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 526 | * GlobalSize (KERNEL.20) |
| 527 | * GlobalSize16 (KERNEL32.32) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 528 | * RETURNS |
| 529 | * Size in bytes of object |
| 530 | * 0: Failure |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 531 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 532 | DWORD WINAPI GlobalSize16( |
| 533 | HGLOBAL16 handle /* [in] Handle of global memory object */ |
| 534 | ) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 535 | TRACE("%04x\n", handle ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 536 | if (!handle) return 0; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 537 | if (!VALID_HANDLE(handle)) |
| 538 | return 0; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 539 | return GET_ARENA_PTR(handle)->size; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 540 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 541 | |
| 542 | |
| 543 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 544 | * GlobalHandle (KERNEL.21) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 545 | * NOTES |
| 546 | * Why is GlobalHandleToSel used here with the sel as input? |
| 547 | * |
| 548 | * RETURNS |
| 549 | * Handle: Success |
| 550 | * NULL: Failure |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 551 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 552 | DWORD WINAPI GlobalHandle16( |
| 553 | WORD sel /* [in] Address of global memory block */ |
| 554 | ) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 555 | TRACE("%04x\n", sel ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 556 | if (!VALID_HANDLE(sel)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 557 | WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 558 | return 0; |
| 559 | } |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 560 | return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) ); |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 561 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 562 | |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 563 | /*********************************************************************** |
| 564 | * GlobalHandleNoRIP (KERNEL.159) |
| 565 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 566 | DWORD WINAPI GlobalHandleNoRIP16( WORD sel ) |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 567 | { |
| 568 | int i; |
| 569 | for (i = globalArenaSize-1 ; i>=0 ; i--) { |
| 570 | if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel) |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 571 | return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) ); |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 572 | } |
| 573 | return 0; |
| 574 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 575 | |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 576 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 577 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 578 | * GlobalFlags (KERNEL.22) |
Patrik Stridvall | b5bb32c | 2002-09-24 18:29:39 +0000 | [diff] [blame] | 579 | * |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 580 | * NOTES |
| 581 | * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid |
| 582 | * handle? |
| 583 | * |
| 584 | * RETURNS |
| 585 | * Value specifying flags and lock count |
| 586 | * GMEM_INVALID_HANDLE: Invalid handle |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 587 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 588 | UINT16 WINAPI GlobalFlags16( |
| 589 | HGLOBAL16 handle /* [in] Handle of global memory object */ |
| 590 | ) { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 591 | GLOBALARENA *pArena; |
| 592 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 593 | TRACE("%04x\n", handle ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 594 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 595 | WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 598 | pArena = GET_ARENA_PTR(handle); |
| 599 | return pArena->lockCount | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 600 | ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) | |
| 601 | ((pArena->base == 0) ? GMEM_DISCARDED : 0); |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 602 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 603 | |
| 604 | |
| 605 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 606 | * LockSegment (KERNEL.23) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 607 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 608 | HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle ) |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 609 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 610 | TRACE("%04x\n", handle ); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 611 | if (handle == (HGLOBAL16)-1) handle = CURRENT_DS; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 612 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 613 | WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 614 | return 0; |
| 615 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 616 | GET_ARENA_PTR(handle)->lockCount++; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 617 | return handle; |
Alexandre Julliard | 75a839a | 1993-07-15 11:13:45 +0000 | [diff] [blame] | 618 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 619 | |
| 620 | |
| 621 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 622 | * UnlockSegment (KERNEL.24) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 623 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 624 | void WINAPI UnlockSegment16( HGLOBAL16 handle ) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 625 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 626 | TRACE("%04x\n", handle ); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 627 | if (handle == (HGLOBAL16)-1) handle = CURRENT_DS; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 628 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 629 | WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 630 | return; |
| 631 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 632 | GET_ARENA_PTR(handle)->lockCount--; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 633 | /* FIXME: this ought to return the lock count in CX (go figure...) */ |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 634 | } |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 635 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 636 | |
| 637 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 638 | * GlobalCompact (KERNEL.25) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 639 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 640 | DWORD WINAPI GlobalCompact16( DWORD desired ) |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 641 | { |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 642 | return GLOBAL_MAX_ALLOC_SIZE; |
Alexandre Julliard | 401710d | 1993-09-04 10:09:32 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 645 | |
| 646 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 647 | * GlobalFreeAll (KERNEL.26) |
| 648 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 649 | void WINAPI GlobalFreeAll16( HGLOBAL16 owner ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 650 | { |
| 651 | DWORD i; |
| 652 | GLOBALARENA *pArena; |
| 653 | |
| 654 | pArena = pGlobalArena; |
| 655 | for (i = 0; i < globalArenaSize; i++, pArena++) |
| 656 | { |
| 657 | if ((pArena->size != 0) && (pArena->hOwner == owner)) |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 658 | GlobalFree16( pArena->handle ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | |
| 663 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 664 | * GlobalWire (KERNEL.111) |
| 665 | * GlobalWire16 (KERNEL32.29) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 666 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 667 | SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle ) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 668 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 669 | return WIN16_GlobalLock16( handle ); |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 670 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 671 | |
| 672 | |
| 673 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 674 | * GlobalUnWire (KERNEL.112) |
| 675 | * GlobalUnWire16 (KERNEL32.30) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 676 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 677 | BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle ) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 678 | { |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 679 | return !GlobalUnlock16( handle ); |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 680 | } |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 681 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 682 | |
| 683 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 684 | * SetSwapAreaSize (KERNEL.106) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 685 | */ |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 686 | LONG WINAPI SetSwapAreaSize16( WORD size ) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 687 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 688 | FIXME("(%d) - stub!\n", size ); |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 689 | return MAKELONG( size, 0xffff ); |
| 690 | } |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 691 | |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 692 | |
| 693 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 694 | * GlobalLRUOldest (KERNEL.163) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 695 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 696 | HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle ) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 697 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 698 | TRACE("%04x\n", handle ); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 699 | if (handle == (HGLOBAL16)-1) handle = CURRENT_DS; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 700 | return handle; |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 703 | |
| 704 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 705 | * GlobalLRUNewest (KERNEL.164) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 706 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 707 | HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle ) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 708 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 709 | TRACE("%04x\n", handle ); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 710 | if (handle == (HGLOBAL16)-1) handle = CURRENT_DS; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 711 | return handle; |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 714 | |
| 715 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 716 | * GetFreeSpace (KERNEL.169) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 717 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 718 | DWORD WINAPI GetFreeSpace16( UINT16 wFlags ) |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 719 | { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 720 | MEMORYSTATUS ms; |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 721 | GlobalMemoryStatus( &ms ); |
| 722 | return ms.dwAvailVirtual; |
Alexandre Julliard | 3a405ba | 1994-10-30 16:25:19 +0000 | [diff] [blame] | 723 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 724 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 725 | /*********************************************************************** |
| 726 | * GlobalDOSAlloc (KERNEL.184) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 727 | * RETURNS |
| 728 | * Address (HW=Paragraph segment; LW=Selector) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 729 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 730 | DWORD WINAPI GlobalDOSAlloc16( |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 731 | DWORD size /* [in] Number of bytes to be allocated */ |
| 732 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 733 | UINT16 uParagraph; |
Ove Kaaven | 7b49914 | 2000-07-28 22:23:59 +0000 | [diff] [blame] | 734 | LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph ); |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 735 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 736 | if( lpBlock ) |
| 737 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 738 | HMODULE16 hModule = GetModuleHandle16("KERNEL"); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 739 | WORD wSelector; |
Andreas Mohr | d23f506 | 2000-10-02 22:16:21 +0000 | [diff] [blame] | 740 | GLOBALARENA *pArena; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 741 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 742 | wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA ); |
Andreas Mohr | d23f506 | 2000-10-02 22:16:21 +0000 | [diff] [blame] | 743 | pArena = GET_ARENA_PTR(wSelector); |
| 744 | pArena->flags |= GA_DOSMEM; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 745 | return MAKELONG(wSelector,uParagraph); |
| 746 | } |
| 747 | return 0; |
| 748 | } |
| 749 | |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 750 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 751 | /*********************************************************************** |
| 752 | * GlobalDOSFree (KERNEL.185) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 753 | * RETURNS |
| 754 | * NULL: Success |
| 755 | * sel: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 756 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 757 | WORD WINAPI GlobalDOSFree16( |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 758 | WORD sel /* [in] Selector */ |
| 759 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 760 | DWORD block = GetSelectorBase(sel); |
| 761 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 762 | if( block && block < 0x100000 ) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 763 | { |
| 764 | LPVOID lpBlock = DOSMEM_MapDosToLinear( block ); |
Ove Kaaven | 7b49914 | 2000-07-28 22:23:59 +0000 | [diff] [blame] | 765 | if( DOSMEM_FreeBlock( lpBlock ) ) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 766 | GLOBAL_FreeBlock( sel ); |
| 767 | sel = 0; |
| 768 | } |
| 769 | return sel; |
| 770 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 771 | |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 772 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 773 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 774 | * GlobalPageLock (KERNEL.191) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 775 | * GlobalSmartPageLock(KERNEL.230) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 776 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 777 | WORD WINAPI GlobalPageLock16( HGLOBAL16 handle ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 778 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 779 | TRACE("%04x\n", handle ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 780 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 781 | WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 782 | return 0; |
| 783 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 784 | return ++(GET_ARENA_PTR(handle)->pageLockCount); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
| 788 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 789 | * GlobalPageUnlock (KERNEL.192) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 790 | * GlobalSmartPageUnlock(KERNEL.231) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 791 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 792 | WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 793 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 794 | TRACE("%04x\n", handle ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 795 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 796 | WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 797 | return 0; |
| 798 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 799 | return --(GET_ARENA_PTR(handle)->pageLockCount); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | |
| 803 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 804 | * GlobalFix (KERNEL.197) |
| 805 | * GlobalFix16 (KERNEL32.27) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 806 | */ |
Ulrich Weigand | 85a7ff4 | 1998-10-11 19:10:10 +0000 | [diff] [blame] | 807 | WORD WINAPI GlobalFix16( HGLOBAL16 handle ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 808 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 809 | TRACE("%04x\n", handle ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 810 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 811 | WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle); |
Ulrich Weigand | 85a7ff4 | 1998-10-11 19:10:10 +0000 | [diff] [blame] | 812 | return 0; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 813 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 814 | GET_ARENA_PTR(handle)->lockCount++; |
Ulrich Weigand | 85a7ff4 | 1998-10-11 19:10:10 +0000 | [diff] [blame] | 815 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 816 | return GlobalHandleToSel16(handle); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
| 820 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 821 | * GlobalUnfix (KERNEL.198) |
| 822 | * GlobalUnfix16 (KERNEL32.28) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 823 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 824 | void WINAPI GlobalUnfix16( HGLOBAL16 handle ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 825 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 826 | TRACE("%04x\n", handle ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 827 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 828 | WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 831 | GET_ARENA_PTR(handle)->lockCount--; |
| 832 | } |
| 833 | |
| 834 | |
| 835 | /*********************************************************************** |
| 836 | * FarSetOwner (KERNEL.403) |
| 837 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 838 | void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 839 | { |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 840 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 841 | WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 842 | return; |
| 843 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 844 | GET_ARENA_PTR(handle)->hOwner = hOwner; |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /*********************************************************************** |
| 849 | * FarGetOwner (KERNEL.404) |
| 850 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 851 | HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 852 | { |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 853 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 854 | WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 855 | return 0; |
| 856 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 857 | return GET_ARENA_PTR(handle)->hOwner; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | |
| 861 | /*********************************************************************** |
| 862 | * GlobalHandleToSel (TOOLHELP.50) |
| 863 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 864 | WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 865 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 866 | if (!handle) return 0; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 867 | if (!VALID_HANDLE(handle)) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 868 | WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 869 | return 0; |
| 870 | } |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 871 | if (!(handle & 7)) |
| 872 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 873 | WARN("Program attempted invalid selector conversion\n" ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 874 | return handle - 1; |
| 875 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 876 | return handle | 7; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 880 | /*********************************************************************** |
| 881 | * GlobalFirst (TOOLHELP.51) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 882 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 883 | BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 884 | { |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 885 | if (wFlags == GLOBAL_LRU) return FALSE; |
| 886 | pGlobal->dwNext = 0; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 887 | return GlobalNext16( pGlobal, wFlags ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 888 | } |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 889 | |
| 890 | |
| 891 | /*********************************************************************** |
| 892 | * GlobalNext (TOOLHELP.52) |
| 893 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 894 | BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 895 | { |
| 896 | GLOBALARENA *pArena; |
| 897 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 898 | if (pGlobal->dwNext >= globalArenaSize) return FALSE; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 899 | pArena = pGlobalArena + pGlobal->dwNext; |
| 900 | if (wFlags == GLOBAL_FREE) /* only free blocks */ |
| 901 | { |
| 902 | int i; |
| 903 | for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++) |
| 904 | if (pArena->size == 0) break; /* block is free */ |
| 905 | if (i >= globalArenaSize) return FALSE; |
| 906 | pGlobal->dwNext = i; |
| 907 | } |
| 908 | |
| 909 | pGlobal->dwAddress = pArena->base; |
| 910 | pGlobal->dwBlockSize = pArena->size; |
| 911 | pGlobal->hBlock = pArena->handle; |
| 912 | pGlobal->wcLock = pArena->lockCount; |
| 913 | pGlobal->wcPageLock = pArena->pageLockCount; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 914 | pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 915 | pGlobal->wHeapPresent = FALSE; |
| 916 | pGlobal->hOwner = pArena->hOwner; |
| 917 | pGlobal->wType = GT_UNKNOWN; |
| 918 | pGlobal->wData = 0; |
| 919 | pGlobal->dwNext++; |
| 920 | return TRUE; |
| 921 | } |
| 922 | |
| 923 | |
| 924 | /*********************************************************************** |
| 925 | * GlobalInfo (TOOLHELP.53) |
| 926 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 927 | BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 928 | { |
| 929 | int i; |
| 930 | GLOBALARENA *pArena; |
| 931 | |
| 932 | pInfo->wcItems = globalArenaSize; |
| 933 | pInfo->wcItemsFree = 0; |
| 934 | pInfo->wcItemsLRU = 0; |
| 935 | for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++) |
| 936 | if (pArena->size == 0) pInfo->wcItemsFree++; |
| 937 | return TRUE; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | /*********************************************************************** |
| 942 | * GlobalEntryHandle (TOOLHELP.54) |
| 943 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 944 | BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 945 | { |
Alexandre Julliard | 03468f7 | 1998-02-15 19:40:49 +0000 | [diff] [blame] | 946 | GLOBALARENA *pArena = GET_ARENA_PTR(hItem); |
| 947 | |
| 948 | pGlobal->dwAddress = pArena->base; |
| 949 | pGlobal->dwBlockSize = pArena->size; |
| 950 | pGlobal->hBlock = pArena->handle; |
| 951 | pGlobal->wcLock = pArena->lockCount; |
| 952 | pGlobal->wcPageLock = pArena->pageLockCount; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 953 | pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner); |
Alexandre Julliard | 03468f7 | 1998-02-15 19:40:49 +0000 | [diff] [blame] | 954 | pGlobal->wHeapPresent = FALSE; |
| 955 | pGlobal->hOwner = pArena->hOwner; |
| 956 | pGlobal->wType = GT_UNKNOWN; |
| 957 | pGlobal->wData = 0; |
| 958 | pGlobal->dwNext++; |
| 959 | return TRUE; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | |
| 963 | /*********************************************************************** |
| 964 | * GlobalEntryModule (TOOLHELP.55) |
| 965 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 966 | BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule, |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 967 | WORD wSeg ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 968 | { |
Andreas Mohr | d23f506 | 2000-10-02 22:16:21 +0000 | [diff] [blame] | 969 | FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg); |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 970 | return FALSE; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | /*********************************************************************** |
| 975 | * MemManInfo (TOOLHELP.72) |
| 976 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 977 | BOOL16 WINAPI MemManInfo16( MEMMANINFO *info ) |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 978 | { |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 979 | MEMORYSTATUS status; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 980 | |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 981 | /* |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 982 | * Not unsurprisingly although the documention says you |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 983 | * _must_ provide the size in the dwSize field, this function |
| 984 | * (under Windows) always fills the structure and returns true. |
| 985 | */ |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 986 | GlobalMemoryStatus( &status ); |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 987 | info->wPageSize = getpagesize(); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 988 | info->dwLargestFreeBlock = status.dwAvailVirtual; |
| 989 | info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize; |
| 990 | info->dwMaxPagesLockable = info->dwMaxPagesAvailable; |
| 991 | info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize; |
| 992 | info->dwTotalUnlockedPages = info->dwTotalLinearSpace; |
| 993 | info->dwFreePages = info->dwMaxPagesAvailable; |
| 994 | info->dwTotalPages = info->dwTotalLinearSpace; |
| 995 | info->dwFreeLinearSpace = info->dwMaxPagesAvailable; |
| 996 | info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize; |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 997 | return TRUE; |
| 998 | } |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 999 | |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 1000 | /*********************************************************************** |
| 1001 | * GetFreeMemInfo (KERNEL.316) |
| 1002 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1003 | DWORD WINAPI GetFreeMemInfo16(void) |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 1004 | { |
| 1005 | MEMMANINFO info; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1006 | MemManInfo16( &info ); |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 1007 | return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable ); |
| 1008 | } |
| 1009 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1010 | /* |
| 1011 | * Win32 Global heap functions (GlobalXXX). |
| 1012 | * These functions included in Win32 for compatibility with 16 bit Windows |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1013 | * Especially the moveable blocks and handles are oldish. |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1014 | * But the ability to directly allocate memory with GPTR and LPTR is widely |
| 1015 | * used. |
| 1016 | * |
| 1017 | * The handle stuff looks horrible, but it's implemented almost like Win95 |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1018 | * does it. |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1019 | * |
| 1020 | */ |
| 1021 | |
| 1022 | #define MAGIC_GLOBAL_USED 0x5342 |
| 1023 | #define GLOBAL_LOCK_MAX 0xFF |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 1024 | #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2)) |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1025 | #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer)) |
| 1026 | #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1)) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1027 | #define ISHANDLE(h) (((DWORD)(h)&2)!=0) |
| 1028 | #define ISPOINTER(h) (((DWORD)(h)&2)==0) |
| 1029 | |
| 1030 | typedef struct __GLOBAL32_INTERN |
| 1031 | { |
| 1032 | WORD Magic; |
| 1033 | LPVOID Pointer WINE_PACKED; |
| 1034 | BYTE Flags; |
| 1035 | BYTE LockCount; |
| 1036 | } GLOBAL32_INTERN, *PGLOBAL32_INTERN; |
| 1037 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1038 | |
| 1039 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1040 | * GlobalAlloc (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1041 | * RETURNS |
| 1042 | * Handle: Success |
| 1043 | * NULL: Failure |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1044 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1045 | HGLOBAL WINAPI GlobalAlloc( |
| 1046 | UINT flags, /* [in] Object allocation attributes */ |
Dmitry Timoshkov | 472d016 | 2002-09-21 01:21:00 +0000 | [diff] [blame] | 1047 | SIZE_T size /* [in] Number of bytes to allocate */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1048 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1049 | PGLOBAL32_INTERN pintern; |
| 1050 | DWORD hpflags; |
| 1051 | LPVOID palloc; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1052 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1053 | if(flags&GMEM_ZEROINIT) |
| 1054 | hpflags=HEAP_ZERO_MEMORY; |
| 1055 | else |
| 1056 | hpflags=0; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1057 | |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1058 | TRACE("() flags=%04x\n", flags ); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1059 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1060 | if((flags & GMEM_MOVEABLE)==0) /* POINTER */ |
| 1061 | { |
| 1062 | palloc=HeapAlloc(GetProcessHeap(), hpflags, size); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1063 | return (HGLOBAL) palloc; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1064 | } |
| 1065 | else /* HANDLE */ |
| 1066 | { |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1067 | /* HeapLock(heap); */ |
| 1068 | |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1069 | pintern=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN)); |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 1070 | if (!pintern) return 0; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1071 | if(size) |
| 1072 | { |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1073 | if (!(palloc=HeapAlloc(GetProcessHeap(), hpflags, size+sizeof(HGLOBAL)))) { |
| 1074 | HeapFree(GetProcessHeap(), 0, pintern); |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 1075 | return 0; |
Eric Pouech | 3bcfb90 | 2000-06-24 12:51:24 +0000 | [diff] [blame] | 1076 | } |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1077 | *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern); |
Patrik Stridvall | a9a671d | 1999-04-25 19:01:52 +0000 | [diff] [blame] | 1078 | pintern->Pointer=(char *) palloc+sizeof(HGLOBAL); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1079 | } |
| 1080 | else |
| 1081 | pintern->Pointer=NULL; |
| 1082 | pintern->Magic=MAGIC_GLOBAL_USED; |
| 1083 | pintern->Flags=flags>>8; |
| 1084 | pintern->LockCount=0; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1085 | |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1086 | /* HeapUnlock(heap); */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1087 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1088 | return INTERN_TO_HANDLE(pintern); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1094 | * GlobalLock (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1095 | * RETURNS |
| 1096 | * Pointer to first byte of block |
| 1097 | * NULL: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1098 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1099 | LPVOID WINAPI GlobalLock( |
| 1100 | HGLOBAL hmem /* [in] Handle of global memory object */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1101 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1102 | PGLOBAL32_INTERN pintern; |
| 1103 | LPVOID palloc; |
| 1104 | |
| 1105 | if(ISPOINTER(hmem)) |
| 1106 | return (LPVOID) hmem; |
| 1107 | |
| 1108 | /* HeapLock(GetProcessHeap()); */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1109 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1110 | pintern=HANDLE_TO_INTERN(hmem); |
| 1111 | if(pintern->Magic==MAGIC_GLOBAL_USED) |
| 1112 | { |
| 1113 | if(pintern->LockCount<GLOBAL_LOCK_MAX) |
| 1114 | pintern->LockCount++; |
| 1115 | palloc=pintern->Pointer; |
| 1116 | } |
| 1117 | else |
| 1118 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 1119 | WARN("invalid handle\n"); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1120 | palloc=(LPVOID) NULL; |
Dmitry Timoshkov | c34fe08 | 2000-11-27 01:33:25 +0000 | [diff] [blame] | 1121 | SetLastError(ERROR_INVALID_HANDLE); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1122 | } |
| 1123 | /* HeapUnlock(GetProcessHeap()); */; |
| 1124 | return palloc; |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1129 | * GlobalUnlock (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1130 | * RETURNS |
| 1131 | * TRUE: Object is still locked |
| 1132 | * FALSE: Object is unlocked |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1133 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1134 | BOOL WINAPI GlobalUnlock( |
| 1135 | HGLOBAL hmem /* [in] Handle of global memory object */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1136 | ) { |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1137 | PGLOBAL32_INTERN pintern; |
| 1138 | BOOL locked; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1139 | |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1140 | if (ISPOINTER(hmem)) return FALSE; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1141 | |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1142 | __TRY |
| 1143 | { |
| 1144 | /* HeapLock(GetProcessHeap()); */ |
| 1145 | pintern=HANDLE_TO_INTERN(hmem); |
| 1146 | if(pintern->Magic==MAGIC_GLOBAL_USED) |
| 1147 | { |
| 1148 | if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0)) |
| 1149 | pintern->LockCount--; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1150 | |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1151 | locked = (pintern->LockCount != 0); |
| 1152 | if (!locked) SetLastError(NO_ERROR); |
| 1153 | } |
| 1154 | else |
| 1155 | { |
| 1156 | WARN("invalid handle\n"); |
| 1157 | SetLastError(ERROR_INVALID_HANDLE); |
| 1158 | locked=FALSE; |
| 1159 | } |
| 1160 | /* HeapUnlock(GetProcessHeap()); */ |
| 1161 | } |
| 1162 | __EXCEPT(page_fault) |
| 1163 | { |
| 1164 | ERR("page fault occurred ! Caused by bug ?\n"); |
| 1165 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1166 | return FALSE; |
| 1167 | } |
| 1168 | __ENDTRY |
| 1169 | return locked; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1174 | * GlobalHandle (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1175 | * Returns the handle associated with the specified pointer. |
| 1176 | * |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1177 | * RETURNS |
| 1178 | * Handle: Success |
| 1179 | * NULL: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1180 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1181 | HGLOBAL WINAPI GlobalHandle( |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1182 | LPCVOID pmem /* [in] Pointer to global memory block */ |
| 1183 | ) { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1184 | HGLOBAL handle; |
Guy Albertelli | 4ef6413 | 2000-01-23 02:25:11 +0000 | [diff] [blame] | 1185 | PGLOBAL32_INTERN maybe_intern; |
| 1186 | LPCVOID test; |
Guy Albertelli | 9867997 | 2000-01-15 21:59:12 +0000 | [diff] [blame] | 1187 | |
| 1188 | if (!pmem) |
| 1189 | { |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1190 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1191 | return 0; |
Guy Albertelli | 9867997 | 2000-01-15 21:59:12 +0000 | [diff] [blame] | 1192 | } |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 1193 | |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1194 | __TRY |
| 1195 | { |
| 1196 | handle = 0; |
Guy Albertelli | 4ef6413 | 2000-01-23 02:25:11 +0000 | [diff] [blame] | 1197 | |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1198 | /* note that if pmem is a pointer to a a block allocated by */ |
| 1199 | /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */ |
| 1200 | /* will fail. */ |
| 1201 | if (ISPOINTER(pmem)) { |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1202 | if (HeapValidate( GetProcessHeap(), 0, pmem )) { |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1203 | handle = (HGLOBAL)pmem; /* valid fixed block */ |
| 1204 | break; |
| 1205 | } |
| 1206 | handle = POINTER_TO_HANDLE(pmem); |
| 1207 | } else |
| 1208 | handle = (HGLOBAL)pmem; |
| 1209 | |
| 1210 | /* Now test handle either passed in or retrieved from pointer */ |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1211 | maybe_intern = HANDLE_TO_INTERN( handle ); |
| 1212 | if (maybe_intern->Magic == MAGIC_GLOBAL_USED) { |
| 1213 | test = maybe_intern->Pointer; |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1214 | if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL *)test)-1 ) && /* obj(-handle) valid arena? */ |
| 1215 | HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */ |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1216 | break; /* valid moveable block */ |
| 1217 | } |
| 1218 | handle = 0; |
| 1219 | SetLastError( ERROR_INVALID_HANDLE ); |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 1220 | } |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1221 | __EXCEPT(page_fault) |
| 1222 | { |
| 1223 | SetLastError( ERROR_INVALID_HANDLE ); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | __ENDTRY |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 1227 | |
Lawson Whitney | 969515d | 2000-10-02 22:27:37 +0000 | [diff] [blame] | 1228 | return handle; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1233 | * GlobalReAlloc (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1234 | * RETURNS |
| 1235 | * Handle: Success |
| 1236 | * NULL: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1237 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1238 | HGLOBAL WINAPI GlobalReAlloc( |
| 1239 | HGLOBAL hmem, /* [in] Handle of global memory object */ |
Dmitry Timoshkov | 472d016 | 2002-09-21 01:21:00 +0000 | [diff] [blame] | 1240 | SIZE_T size, /* [in] New size of block */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1241 | UINT flags /* [in] How to reallocate object */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1242 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1243 | LPVOID palloc; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1244 | HGLOBAL hnew; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1245 | PGLOBAL32_INTERN pintern; |
Alexandre Julliard | d10ca9c | 1999-09-04 11:26:56 +0000 | [diff] [blame] | 1246 | DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1247 | |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 1248 | hnew = 0; |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1249 | /* HeapLock(heap); */ |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1250 | if(flags & GMEM_MODIFY) /* modify flags */ |
| 1251 | { |
| 1252 | if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE)) |
| 1253 | { |
| 1254 | /* make a fixed block moveable |
| 1255 | * actually only NT is able to do this. But it's soo simple |
| 1256 | */ |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1257 | if (hmem == 0) |
| 1258 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 1259 | ERR("GlobalReAlloc32 with null handle!\n"); |
Alexandre Julliard | 0ad42fa | 1999-01-31 15:04:42 +0000 | [diff] [blame] | 1260 | SetLastError( ERROR_NOACCESS ); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1263 | size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1264 | hnew=GlobalAlloc( flags, size); |
| 1265 | palloc=GlobalLock(hnew); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1266 | memcpy(palloc, (LPVOID) hmem, size); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1267 | GlobalUnlock(hnew); |
| 1268 | GlobalFree(hmem); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1269 | } |
| 1270 | else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE)) |
| 1271 | { |
| 1272 | /* change the flags to make our block "discardable" */ |
| 1273 | pintern=HANDLE_TO_INTERN(hmem); |
| 1274 | pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8); |
| 1275 | hnew=hmem; |
| 1276 | } |
| 1277 | else |
| 1278 | { |
| 1279 | SetLastError(ERROR_INVALID_PARAMETER); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 1280 | hnew = 0; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | else |
| 1284 | { |
| 1285 | if(ISPOINTER(hmem)) |
| 1286 | { |
| 1287 | /* reallocate fixed memory */ |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1288 | hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1289 | } |
| 1290 | else |
| 1291 | { |
| 1292 | /* reallocate a moveable block */ |
| 1293 | pintern=HANDLE_TO_INTERN(hmem); |
Ulrich Weigand | 2e7008c | 2000-10-19 20:24:25 +0000 | [diff] [blame] | 1294 | |
| 1295 | #if 0 |
| 1296 | /* Apparently Windows doesn't care whether the handle is locked at this point */ |
| 1297 | /* See also the same comment in GlobalFree() */ |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1298 | if(pintern->LockCount>1) { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 1299 | ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1300 | SetLastError(ERROR_INVALID_HANDLE); |
Ulrich Weigand | 2e7008c | 2000-10-19 20:24:25 +0000 | [diff] [blame] | 1301 | } else |
| 1302 | #endif |
| 1303 | if(size!=0) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1304 | { |
| 1305 | hnew=hmem; |
| 1306 | if(pintern->Pointer) |
| 1307 | { |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1308 | if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags, |
Huw D M Davies | c43fdb7 | 2000-04-13 15:57:06 +0000 | [diff] [blame] | 1309 | (char *) pintern->Pointer-sizeof(HGLOBAL), |
| 1310 | size+sizeof(HGLOBAL))) == NULL) |
| 1311 | return 0; /* Block still valid */ |
Patrik Stridvall | a9a671d | 1999-04-25 19:01:52 +0000 | [diff] [blame] | 1312 | pintern->Pointer=(char *) palloc+sizeof(HGLOBAL); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1313 | } |
| 1314 | else |
| 1315 | { |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1316 | if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+sizeof(HGLOBAL))) |
Huw D M Davies | c43fdb7 | 2000-04-13 15:57:06 +0000 | [diff] [blame] | 1317 | == NULL) |
| 1318 | return 0; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1319 | *(HGLOBAL *)palloc=hmem; |
Patrik Stridvall | a9a671d | 1999-04-25 19:01:52 +0000 | [diff] [blame] | 1320 | pintern->Pointer=(char *) palloc+sizeof(HGLOBAL); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | else |
| 1324 | { |
| 1325 | if(pintern->Pointer) |
| 1326 | { |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1327 | HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-sizeof(HGLOBAL)); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1328 | pintern->Pointer=NULL; |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | } |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1333 | /* HeapUnlock(heap); */ |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1334 | return hnew; |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1339 | * GlobalFree (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1340 | * RETURNS |
| 1341 | * NULL: Success |
| 1342 | * Handle: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1343 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1344 | HGLOBAL WINAPI GlobalFree( |
| 1345 | HGLOBAL hmem /* [in] Handle of global memory object */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1346 | ) { |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1347 | PGLOBAL32_INTERN pintern; |
| 1348 | HGLOBAL hreturned; |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1349 | |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1350 | __TRY |
| 1351 | { |
| 1352 | hreturned = 0; |
| 1353 | if(ISPOINTER(hmem)) /* POINTER */ |
| 1354 | { |
| 1355 | if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0; |
| 1356 | } |
| 1357 | else /* HANDLE */ |
| 1358 | { |
| 1359 | /* HeapLock(heap); */ |
| 1360 | pintern=HANDLE_TO_INTERN(hmem); |
Guy Albertelli | 4ef6413 | 2000-01-23 02:25:11 +0000 | [diff] [blame] | 1361 | |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1362 | if(pintern->Magic==MAGIC_GLOBAL_USED) |
| 1363 | { |
Guy Albertelli | 4ef6413 | 2000-01-23 02:25:11 +0000 | [diff] [blame] | 1364 | |
Andreas Mohr | 55a14ed | 2001-11-20 20:26:35 +0000 | [diff] [blame] | 1365 | /* WIN98 does not make this test. That is you can free a */ |
| 1366 | /* block you have not unlocked. Go figure!! */ |
| 1367 | /* if(pintern->LockCount!=0) */ |
| 1368 | /* SetLastError(ERROR_INVALID_HANDLE); */ |
| 1369 | |
| 1370 | if(pintern->Pointer) |
| 1371 | if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-sizeof(HGLOBAL))) |
| 1372 | hreturned=hmem; |
| 1373 | if(!HeapFree(GetProcessHeap(), 0, pintern)) |
| 1374 | hreturned=hmem; |
| 1375 | } |
| 1376 | /* HeapUnlock(heap); */ |
| 1377 | } |
| 1378 | } |
| 1379 | __EXCEPT(page_fault) |
| 1380 | { |
| 1381 | ERR("page fault occurred ! Caused by bug ?\n"); |
| 1382 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1383 | return hmem; |
| 1384 | } |
| 1385 | __ENDTRY |
| 1386 | return hreturned; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1391 | * GlobalSize (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1392 | * RETURNS |
| 1393 | * Size in bytes of the global memory object |
| 1394 | * 0: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1395 | */ |
Dmitry Timoshkov | 472d016 | 2002-09-21 01:21:00 +0000 | [diff] [blame] | 1396 | SIZE_T WINAPI GlobalSize( |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1397 | HGLOBAL hmem /* [in] Handle of global memory object */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1398 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1399 | DWORD retval; |
| 1400 | PGLOBAL32_INTERN pintern; |
| 1401 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1402 | if(ISPOINTER(hmem)) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1403 | { |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1404 | retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1405 | } |
| 1406 | else |
| 1407 | { |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1408 | /* HeapLock(heap); */ |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1409 | pintern=HANDLE_TO_INTERN(hmem); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1410 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1411 | if(pintern->Magic==MAGIC_GLOBAL_USED) |
| 1412 | { |
Guy Albertelli | 3db8e19 | 1999-02-14 17:38:24 +0000 | [diff] [blame] | 1413 | if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */ |
| 1414 | return 0; |
Alexandre Julliard | 61d32b4 | 2001-02-23 01:35:36 +0000 | [diff] [blame] | 1415 | retval=HeapSize(GetProcessHeap(), 0, |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1416 | (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4; |
Rein Klazes | eaec1ee | 1999-07-11 13:50:24 +0000 | [diff] [blame] | 1417 | if (retval == 0xffffffff-4) retval = 0; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1418 | } |
| 1419 | else |
| 1420 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 1421 | WARN("invalid handle\n"); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1422 | retval=0; |
| 1423 | } |
Noel Borthwick | 83579c8 | 1999-07-24 12:18:04 +0000 | [diff] [blame] | 1424 | /* HeapUnlock(heap); */ |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1425 | } |
Guy Albertelli | 3db8e19 | 1999-02-14 17:38:24 +0000 | [diff] [blame] | 1426 | /* HeapSize returns 0xffffffff on failure */ |
| 1427 | if (retval == 0xffffffff) retval = 0; |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1428 | return retval; |
| 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1433 | * GlobalWire (KERNEL32.@) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1434 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1435 | LPVOID WINAPI GlobalWire(HGLOBAL hmem) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1436 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1437 | return GlobalLock( hmem ); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1442 | * GlobalUnWire (KERNEL32.@) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1443 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1444 | BOOL WINAPI GlobalUnWire(HGLOBAL hmem) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1445 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1446 | return GlobalUnlock( hmem); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1451 | * GlobalFix (KERNEL32.@) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1452 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1453 | VOID WINAPI GlobalFix(HGLOBAL hmem) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1454 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1455 | GlobalLock( hmem ); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1460 | * GlobalUnfix (KERNEL32.@) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1461 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1462 | VOID WINAPI GlobalUnfix(HGLOBAL hmem) |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1463 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1464 | GlobalUnlock( hmem); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1469 | * GlobalFlags (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1470 | * Returns information about the specified global memory object |
| 1471 | * |
| 1472 | * NOTES |
| 1473 | * Should this return GMEM_INVALID_HANDLE on invalid handle? |
| 1474 | * |
| 1475 | * RETURNS |
| 1476 | * Value specifying allocation flags and lock count |
| 1477 | * GMEM_INVALID_HANDLE: Failure |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1478 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1479 | UINT WINAPI GlobalFlags( |
| 1480 | HGLOBAL hmem /* [in] Handle to global memory object */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1481 | ) { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1482 | DWORD retval; |
| 1483 | PGLOBAL32_INTERN pintern; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1484 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1485 | if(ISPOINTER(hmem)) |
| 1486 | { |
| 1487 | retval=0; |
| 1488 | } |
| 1489 | else |
| 1490 | { |
| 1491 | /* HeapLock(GetProcessHeap()); */ |
| 1492 | pintern=HANDLE_TO_INTERN(hmem); |
| 1493 | if(pintern->Magic==MAGIC_GLOBAL_USED) |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1494 | { |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1495 | retval=pintern->LockCount + (pintern->Flags<<8); |
| 1496 | if(pintern->Pointer==0) |
| 1497 | retval|= GMEM_DISCARDED; |
| 1498 | } |
| 1499 | else |
| 1500 | { |
Andrew John Hughes | ed800c6 | 2002-11-21 03:45:01 +0000 | [diff] [blame] | 1501 | WARN("Invalid handle: %p\n", hmem); |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 1502 | retval=0; |
| 1503 | } |
| 1504 | /* HeapUnlock(GetProcessHeap()); */ |
| 1505 | } |
| 1506 | return retval; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1511 | * GlobalCompact (KERNEL32.@) |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1512 | */ |
Dmitry Timoshkov | 472d016 | 2002-09-21 01:21:00 +0000 | [diff] [blame] | 1513 | SIZE_T WINAPI GlobalCompact( DWORD minfree ) |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1514 | { |
| 1515 | return 0; /* GlobalCompact does nothing in Win32 */ |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1520 | * GlobalMemoryStatus (KERNEL32.@) |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1521 | * RETURNS |
| 1522 | * None |
Alexandre Julliard | 0e270f4 | 1996-08-24 18:26:35 +0000 | [diff] [blame] | 1523 | */ |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1524 | VOID WINAPI GlobalMemoryStatus( |
| 1525 | LPMEMORYSTATUS lpmem |
| 1526 | ) { |
Marcus Meissner | 6189c19 | 2000-03-04 19:19:15 +0000 | [diff] [blame] | 1527 | static MEMORYSTATUS cached_memstatus; |
| 1528 | static int cache_lastchecked = 0; |
Francois Gouget | ea5924c | 2000-10-28 00:34:29 +0000 | [diff] [blame] | 1529 | SYSTEM_INFO si; |
Patrik Stridvall | 7e9913f | 2000-03-08 18:25:22 +0000 | [diff] [blame] | 1530 | #ifdef linux |
Marcus Meissner | 6189c19 | 2000-03-04 19:19:15 +0000 | [diff] [blame] | 1531 | FILE *f; |
Patrik Stridvall | 7e9913f | 2000-03-08 18:25:22 +0000 | [diff] [blame] | 1532 | #endif |
Olivier Houchard | f41ecf3 | 2002-05-07 01:50:02 +0000 | [diff] [blame] | 1533 | #ifdef __FreeBSD__ |
| 1534 | int *tmp; |
| 1535 | int size_sys; |
| 1536 | #endif |
Marcus Meissner | 6189c19 | 2000-03-04 19:19:15 +0000 | [diff] [blame] | 1537 | if (time(NULL)==cache_lastchecked) { |
| 1538 | memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS)); |
| 1539 | return; |
| 1540 | } |
| 1541 | cache_lastchecked = time(NULL); |
| 1542 | |
Olivier Houchard | f41ecf3 | 2002-05-07 01:50:02 +0000 | [diff] [blame] | 1543 | lpmem->dwMemoryLoad = 0; |
| 1544 | lpmem->dwTotalPhys = 16*1024*1024; |
| 1545 | lpmem->dwAvailPhys = 16*1024*1024; |
| 1546 | lpmem->dwTotalPageFile = 16*1024*1024; |
| 1547 | lpmem->dwAvailPageFile = 16*1024*1024; |
| 1548 | |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1549 | #ifdef linux |
Marcus Meissner | 6189c19 | 2000-03-04 19:19:15 +0000 | [diff] [blame] | 1550 | f = fopen( "/proc/meminfo", "r" ); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1551 | if (f) |
| 1552 | { |
| 1553 | char buffer[256]; |
Stephen Crowley | 59c4a32 | 1998-11-24 20:41:02 +0000 | [diff] [blame] | 1554 | int total, used, free, shared, buffers, cached; |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1555 | |
Stephen Crowley | 59c4a32 | 1998-11-24 20:41:02 +0000 | [diff] [blame] | 1556 | lpmem->dwLength = sizeof(MEMORYSTATUS); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1557 | lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0; |
| 1558 | lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0; |
| 1559 | while (fgets( buffer, sizeof(buffer), f )) |
| 1560 | { |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1561 | /* old style /proc/meminfo ... */ |
Stephen Crowley | 59c4a32 | 1998-11-24 20:41:02 +0000 | [diff] [blame] | 1562 | if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached )) |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1563 | { |
| 1564 | lpmem->dwTotalPhys += total; |
Stephen Crowley | 59c4a32 | 1998-11-24 20:41:02 +0000 | [diff] [blame] | 1565 | lpmem->dwAvailPhys += free + buffers + cached; |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1566 | } |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1567 | if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free )) |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1568 | { |
| 1569 | lpmem->dwTotalPageFile += total; |
| 1570 | lpmem->dwAvailPageFile += free; |
| 1571 | } |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1572 | |
| 1573 | /* new style /proc/meminfo ... */ |
| 1574 | if (sscanf(buffer, "MemTotal: %d", &total)) |
| 1575 | lpmem->dwTotalPhys = total*1024; |
| 1576 | if (sscanf(buffer, "MemFree: %d", &free)) |
| 1577 | lpmem->dwAvailPhys = free*1024; |
| 1578 | if (sscanf(buffer, "SwapTotal: %d", &total)) |
| 1579 | lpmem->dwTotalPageFile = total*1024; |
| 1580 | if (sscanf(buffer, "SwapFree: %d", &free)) |
| 1581 | lpmem->dwAvailPageFile = free*1024; |
Stephen Crowley | 59c4a32 | 1998-11-24 20:41:02 +0000 | [diff] [blame] | 1582 | if (sscanf(buffer, "Buffers: %d", &buffers)) |
| 1583 | lpmem->dwAvailPhys += buffers*1024; |
| 1584 | if (sscanf(buffer, "Cached: %d", &cached)) |
| 1585 | lpmem->dwAvailPhys += cached*1024; |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1586 | } |
| 1587 | fclose( f ); |
| 1588 | |
| 1589 | if (lpmem->dwTotalPhys) |
| 1590 | { |
Francois Gouget | ea5924c | 2000-10-28 00:34:29 +0000 | [diff] [blame] | 1591 | DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile; |
| 1592 | DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile; |
| 1593 | lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical) |
| 1594 | / (TotalPhysical / 100); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1595 | } |
Marcus Meissner | fddbcf3 | 2000-03-07 12:24:58 +0000 | [diff] [blame] | 1596 | } |
Olivier Houchard | f41ecf3 | 2002-05-07 01:50:02 +0000 | [diff] [blame] | 1597 | #elif defined(__FreeBSD__) |
| 1598 | sysctlbyname("hw.physmem", NULL, &size_sys, NULL, 0); |
| 1599 | tmp = malloc(size_sys * sizeof(int)); |
| 1600 | sysctlbyname("hw.physmem", tmp, &size_sys, NULL, 0); |
| 1601 | if (tmp && *tmp) |
| 1602 | { |
| 1603 | lpmem->dwTotalPhys = *tmp; |
| 1604 | free(tmp); |
| 1605 | sysctlbyname("hw.usermem", NULL, &size_sys, NULL, 0); |
| 1606 | tmp = malloc(size_sys * sizeof(int)); |
| 1607 | sysctlbyname("hw.usermem", tmp, &size_sys, NULL, 0); |
| 1608 | if (tmp && *tmp) |
| 1609 | { |
| 1610 | lpmem->dwAvailPhys = *tmp; |
| 1611 | lpmem->dwTotalPageFile = *tmp; |
| 1612 | lpmem->dwAvailPageFile = *tmp; |
| 1613 | lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys; |
| 1614 | } else |
| 1615 | { |
| 1616 | lpmem->dwAvailPhys = lpmem->dwTotalPhys; |
| 1617 | lpmem->dwTotalPageFile = lpmem->dwTotalPhys; |
| 1618 | lpmem->dwAvailPageFile = lpmem->dwTotalPhys; |
| 1619 | lpmem->dwMemoryLoad = 0; |
| 1620 | } |
| 1621 | free(tmp); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1622 | |
Olivier Houchard | f41ecf3 | 2002-05-07 01:50:02 +0000 | [diff] [blame] | 1623 | } |
| 1624 | #endif |
Francois Gouget | 1f0e33c | 2002-11-13 19:48:07 +0000 | [diff] [blame] | 1625 | /* Some applications (e.g. QuickTime 6) crash if we tell them there |
| 1626 | * is more than 2GB of physical memory. |
| 1627 | */ |
| 1628 | if (lpmem->dwTotalPhys>2U*1024*1024*1024) |
| 1629 | { |
| 1630 | lpmem->dwTotalPhys=2U*1024*1024*1024; |
| 1631 | lpmem->dwAvailPhys=2U*1024*1024*1024; |
| 1632 | } |
| 1633 | |
Olivier Houchard | f41ecf3 | 2002-05-07 01:50:02 +0000 | [diff] [blame] | 1634 | /* FIXME: should do something for other systems */ |
Francois Gouget | ea5924c | 2000-10-28 00:34:29 +0000 | [diff] [blame] | 1635 | GetSystemInfo(&si); |
Gregg Mattinson | 7c4cb51 | 2002-07-03 21:10:43 +0000 | [diff] [blame] | 1636 | lpmem->dwTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress; |
Francois Gouget | ea5924c | 2000-10-28 00:34:29 +0000 | [diff] [blame] | 1637 | /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */ |
| 1638 | lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024; |
Marcus Meissner | fddbcf3 | 2000-03-07 12:24:58 +0000 | [diff] [blame] | 1639 | memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS)); |
Mike McCormack | c56a79a | 2001-01-04 19:52:51 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* it appears some memory display programs want to divide by these values */ |
| 1642 | if(lpmem->dwTotalPageFile==0) |
| 1643 | lpmem->dwTotalPageFile++; |
| 1644 | |
| 1645 | if(lpmem->dwAvailPageFile==0) |
| 1646 | lpmem->dwAvailPageFile++; |
Andreas Mohr | cacbe21 | 2002-04-02 19:40:49 +0000 | [diff] [blame] | 1647 | |
| 1648 | TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld," |
| 1649 | " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n", |
| 1650 | lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys, |
| 1651 | lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual, |
| 1652 | lpmem->dwAvailVirtual); |
Alexandre Julliard | 0e270f4 | 1996-08-24 18:26:35 +0000 | [diff] [blame] | 1653 | } |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1654 | |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1655 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 1656 | * A20Proc (KERNEL.165) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 1657 | * A20_Proc (SYSTEM.20) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1658 | */ |
| 1659 | void WINAPI A20Proc16( WORD unused ) |
| 1660 | { |
| 1661 | /* this is also a NOP in Windows */ |
| 1662 | } |
| 1663 | |
| 1664 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 1665 | * LimitEMSPages (KERNEL.156) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1666 | */ |
| 1667 | DWORD WINAPI LimitEMSPages16( DWORD unused ) |
| 1668 | { |
| 1669 | return 0; |
| 1670 | } |