os/2 bits for SDL_malloc.c (in case we need it some day) SDL-1.2
authorOzkan Sezer <sezeroz@gmail.com>
Sat, 14 Sep 2019 17:00:40 +0300
branchSDL-1.2
changeset 1307658a05dc1e7de
parent 13075 b4a4e1683304
child 13077 cd46eb772bee
os/2 bits for SDL_malloc.c (in case we need it some day)
src/stdlib/SDL_malloc.c
     1.1 --- a/src/stdlib/SDL_malloc.c	Sat Sep 14 17:00:02 2019 +0300
     1.2 +++ b/src/stdlib/SDL_malloc.c	Sat Sep 14 17:00:40 2019 +0300
     1.3 @@ -496,6 +496,14 @@
     1.4  #define MMAP_CLEARS 0 /* WINCE and some others apparently don't clear */
     1.5  #endif  /* WIN32 */
     1.6  
     1.7 +#ifdef __OS2__
     1.8 +#define INCL_DOS
     1.9 +#include <os2.h>
    1.10 +#define HAVE_MMAP 1
    1.11 +#define HAVE_MORECORE 0
    1.12 +#define LACKS_SYS_MMAN_H
    1.13 +#endif  /* __OS2__ */
    1.14 +
    1.15  #if defined(DARWIN) || defined(_DARWIN)
    1.16  /* Mac OSX docs advise not to use sbrk; it seems better to use mmap */
    1.17  #ifndef HAVE_MORECORE
    1.18 @@ -1334,7 +1342,7 @@
    1.19  #define IS_MMAPPED_BIT       (SIZE_T_ONE)
    1.20  #define USE_MMAP_BIT         (SIZE_T_ONE)
    1.21  
    1.22 -#ifndef WIN32
    1.23 +#if !defined(WIN32) && !defined (__OS2__)
    1.24  #define CALL_MUNMAP(a, s)    munmap((a), (s))
    1.25  #define MMAP_PROT            (PROT_READ|PROT_WRITE)
    1.26  #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
    1.27 @@ -1357,6 +1365,42 @@
    1.28  #endif /* MAP_ANONYMOUS */
    1.29  
    1.30  #define DIRECT_MMAP(s)       CALL_MMAP(s)
    1.31 +
    1.32 +#elif defined(__OS2__)
    1.33 +
    1.34 +/* OS/2 MMAP via DosAllocMem */
    1.35 +static void* os2mmap(size_t size) {
    1.36 +  void* ptr;
    1.37 +  if (DosAllocMem(&ptr, size, OBJ_ANY|PAG_COMMIT|PAG_READ|PAG_WRITE) &&
    1.38 +      DosAllocMem(&ptr, size, PAG_COMMIT|PAG_READ|PAG_WRITE))
    1.39 +    return MFAIL;
    1.40 +  return ptr;
    1.41 +}
    1.42 +
    1.43 +#define os2direct_mmap(n)     os2mmap(n)
    1.44 +
    1.45 +/* This function supports releasing coalesed segments */
    1.46 +static int os2munmap(void* ptr, size_t size) {
    1.47 +  while (size) {
    1.48 +    ULONG ulSize = size;
    1.49 +    ULONG ulFlags = 0;
    1.50 +    if (DosQueryMem(ptr, &ulSize, &ulFlags) != 0)
    1.51 +      return -1;
    1.52 +    if ((ulFlags & PAG_BASE) == 0 ||(ulFlags & PAG_COMMIT) == 0 ||
    1.53 +        ulSize > size)
    1.54 +      return -1;
    1.55 +    if (DosFreeMem(ptr) != 0)
    1.56 +      return -1;
    1.57 +    ptr = ( void * ) ( ( char * ) ptr + ulSize );
    1.58 +    size -= ulSize;
    1.59 +  }
    1.60 +  return 0;
    1.61 +}
    1.62 +
    1.63 +#define CALL_MMAP(s)         os2mmap(s)
    1.64 +#define CALL_MUNMAP(a, s)    os2munmap((a), (s))
    1.65 +#define DIRECT_MMAP(s)       os2direct_mmap(s)
    1.66 +
    1.67  #else /* WIN32 */
    1.68  
    1.69  /* Win32 MMAP via VirtualAlloc */
    1.70 @@ -1433,7 +1477,7 @@
    1.71      unique mparams values are initialized only once.
    1.72  */
    1.73  
    1.74 -#ifndef WIN32
    1.75 +#if !defined(WIN32) && !defined(__OS2__)
    1.76  /* By default use posix locks */
    1.77  #include <pthread.h>
    1.78  #define MLOCK_T pthread_mutex_t
    1.79 @@ -1447,6 +1491,16 @@
    1.80  
    1.81  static MLOCK_T magic_init_mutex = PTHREAD_MUTEX_INITIALIZER;
    1.82  
    1.83 +#elif defined(__OS2__)
    1.84 +#define MLOCK_T HMTX
    1.85 +#define INITIAL_LOCK(l)      DosCreateMutexSem(0, l, 0, FALSE)
    1.86 +#define ACQUIRE_LOCK(l)      DosRequestMutexSem(*l, SEM_INDEFINITE_WAIT)
    1.87 +#define RELEASE_LOCK(l)      DosReleaseMutexSem(*l)
    1.88 +#if HAVE_MORECORE
    1.89 +static MLOCK_T morecore_mutex;
    1.90 +#endif /* HAVE_MORECORE */
    1.91 +static MLOCK_T magic_init_mutex;
    1.92 +
    1.93  #else /* WIN32 */
    1.94  /*
    1.95     Because lock-protected regions have bounded times, and there
    1.96 @@ -2502,10 +2556,15 @@
    1.97      }
    1.98      RELEASE_MAGIC_INIT_LOCK();
    1.99  
   1.100 -#ifndef WIN32
   1.101 +#if !defined(WIN32) && !defined(__OS2__)
   1.102      mparams.page_size = malloc_getpagesize;
   1.103      mparams.granularity = ((DEFAULT_GRANULARITY != 0)?
   1.104                             DEFAULT_GRANULARITY : mparams.page_size);
   1.105 +#elif defined (__OS2__)
   1.106 +    /* if low-memory is used, os2munmap() would break
   1.107 +       if it were anything other than 64k */
   1.108 +    mparams.page_size = 4096u;
   1.109 +    mparams.granularity = 65536u;
   1.110  #else /* WIN32 */
   1.111      {
   1.112        SYSTEM_INFO system_info;