goat3d

diff libs/openctm/liblzma/Alloc.c @ 14:188c697b3b49

- added a document describing the goat3d file format chunk hierarchy - started an alternative XML-based file format - added the openctm library
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 04:47:05 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/openctm/liblzma/Alloc.c	Thu Sep 26 04:47:05 2013 +0300
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* Alloc.c -- Memory allocation functions
     1.5 +2008-09-24
     1.6 +Igor Pavlov
     1.7 +Public domain */
     1.8 +
     1.9 +#ifdef _WIN32
    1.10 +#include <windows.h>
    1.11 +#endif
    1.12 +#include <stdlib.h>
    1.13 +
    1.14 +#include "Alloc.h"
    1.15 +
    1.16 +/* #define _SZ_ALLOC_DEBUG */
    1.17 +
    1.18 +/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
    1.19 +#ifdef _SZ_ALLOC_DEBUG
    1.20 +#include <stdio.h>
    1.21 +int g_allocCount = 0;
    1.22 +int g_allocCountMid = 0;
    1.23 +int g_allocCountBig = 0;
    1.24 +#endif
    1.25 +
    1.26 +void *MyAlloc(size_t size)
    1.27 +{
    1.28 +  if (size == 0)
    1.29 +    return 0;
    1.30 +  #ifdef _SZ_ALLOC_DEBUG
    1.31 +  {
    1.32 +    void *p = malloc(size);
    1.33 +    fprintf(stderr, "\nAlloc %10d bytes, count = %10d,  addr = %8X", size, g_allocCount++, (unsigned)p);
    1.34 +    return p;
    1.35 +  }
    1.36 +  #else
    1.37 +  return malloc(size);
    1.38 +  #endif
    1.39 +}
    1.40 +
    1.41 +void MyFree(void *address)
    1.42 +{
    1.43 +  #ifdef _SZ_ALLOC_DEBUG
    1.44 +  if (address != 0)
    1.45 +    fprintf(stderr, "\nFree; count = %10d,  addr = %8X", --g_allocCount, (unsigned)address);
    1.46 +  #endif
    1.47 +  free(address);
    1.48 +}
    1.49 +
    1.50 +#ifdef _WIN32
    1.51 +
    1.52 +void *MidAlloc(size_t size)
    1.53 +{
    1.54 +  if (size == 0)
    1.55 +    return 0;
    1.56 +  #ifdef _SZ_ALLOC_DEBUG
    1.57 +  fprintf(stderr, "\nAlloc_Mid %10d bytes;  count = %10d", size, g_allocCountMid++);
    1.58 +  #endif
    1.59 +  return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
    1.60 +}
    1.61 +
    1.62 +void MidFree(void *address)
    1.63 +{
    1.64 +  #ifdef _SZ_ALLOC_DEBUG
    1.65 +  if (address != 0)
    1.66 +    fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
    1.67 +  #endif
    1.68 +  if (address == 0)
    1.69 +    return;
    1.70 +  VirtualFree(address, 0, MEM_RELEASE);
    1.71 +}
    1.72 +
    1.73 +#ifndef MEM_LARGE_PAGES
    1.74 +#undef _7ZIP_LARGE_PAGES
    1.75 +#endif
    1.76 +
    1.77 +#ifdef _7ZIP_LARGE_PAGES
    1.78 +SIZE_T g_LargePageSize = 0;
    1.79 +typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
    1.80 +#endif
    1.81 +
    1.82 +void SetLargePageSize()
    1.83 +{
    1.84 +  #ifdef _7ZIP_LARGE_PAGES
    1.85 +  SIZE_T size = 0;
    1.86 +  GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
    1.87 +        GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
    1.88 +  if (largePageMinimum == 0)
    1.89 +    return;
    1.90 +  size = largePageMinimum();
    1.91 +  if (size == 0 || (size & (size - 1)) != 0)
    1.92 +    return;
    1.93 +  g_LargePageSize = size;
    1.94 +  #endif
    1.95 +}
    1.96 +
    1.97 +
    1.98 +void *BigAlloc(size_t size)
    1.99 +{
   1.100 +  if (size == 0)
   1.101 +    return 0;
   1.102 +  #ifdef _SZ_ALLOC_DEBUG
   1.103 +  fprintf(stderr, "\nAlloc_Big %10d bytes;  count = %10d", size, g_allocCountBig++);
   1.104 +  #endif
   1.105 +  
   1.106 +  #ifdef _7ZIP_LARGE_PAGES
   1.107 +  if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
   1.108 +  {
   1.109 +    void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
   1.110 +        MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
   1.111 +    if (res != 0)
   1.112 +      return res;
   1.113 +  }
   1.114 +  #endif
   1.115 +  return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
   1.116 +}
   1.117 +
   1.118 +void BigFree(void *address)
   1.119 +{
   1.120 +  #ifdef _SZ_ALLOC_DEBUG
   1.121 +  if (address != 0)
   1.122 +    fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
   1.123 +  #endif
   1.124 +  
   1.125 +  if (address == 0)
   1.126 +    return;
   1.127 +  VirtualFree(address, 0, MEM_RELEASE);
   1.128 +}
   1.129 +
   1.130 +#endif