home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / xp / xp_alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.8 KB  |  108 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #include "xp_mem.h"
  21. #include "xp_mcom.h"
  22.  
  23. #define ALLOCSIZE    4096
  24.  
  25. void
  26. XP_InitAllocStructInfo(XP_AllocStructInfo* info, int size)
  27. {
  28.   info->size = XP_INITIALIZE_ALLOCSTRUCTINFO(size);
  29.   info->curchunk = NULL;
  30.   info->leftinchunk = 0;
  31.   info->firstfree = NULL;
  32.   info->firstchunk = NULL;
  33.   info->numalloced = 0;
  34. }
  35.  
  36.  
  37. void*
  38. XP_AllocStruct(XP_AllocStructInfo* info)
  39. {
  40.   void* result;
  41.   if (info->firstfree) {
  42.     result = info->firstfree;
  43.     info->firstfree = *((void**) info->firstfree);
  44.   } else {
  45.     if (info->curchunk == NULL) {
  46.       info->leftinchunk = (ALLOCSIZE - sizeof(void*)) / info->size;
  47.       if (info->leftinchunk < 1) info->leftinchunk = 1;
  48.       info->curchunk = XP_ALLOC(info->size * info->leftinchunk +
  49.                 sizeof(void*));
  50.       while (info->curchunk == NULL) {
  51.     info->leftinchunk /= 2;
  52.     if (info->leftinchunk < 1) return NULL;
  53.     info->curchunk = XP_ALLOC(info->size * info->leftinchunk +
  54.                   sizeof(void*));
  55.       }
  56.       *((void**) info->curchunk) = info->firstchunk;
  57.       info->firstchunk = info->curchunk;
  58.       info->curchunk = ((uint8*) info->curchunk) + sizeof(void**);
  59.     }
  60.  
  61.     result = info->curchunk;
  62.     info->leftinchunk--;
  63.     if (info->leftinchunk <= 0) {
  64.       info->curchunk = NULL;
  65.     } else {
  66.       info->curchunk = ((uint8*) info->curchunk) + info->size;
  67.     }
  68.   }
  69.  
  70.   info->numalloced++;
  71.   return result;
  72. }
  73.  
  74. void*
  75. XP_AllocStructZero(XP_AllocStructInfo* info)
  76. {
  77.   void* result = XP_AllocStruct(info);
  78.   if (result) {
  79.     XP_BZERO(result, info->size);
  80.   }
  81.   return result;
  82. }
  83.  
  84. void
  85. XP_FreeStruct(XP_AllocStructInfo* info, void* ptr)
  86. {
  87.   *((void**) ptr) = info->firstfree;
  88.   info->firstfree = ptr;
  89.   info->numalloced--;
  90.   if (info->numalloced <= 0) {
  91.     XP_FreeAllStructs(info);
  92.   }
  93. }
  94.  
  95. void
  96. XP_FreeAllStructs(XP_AllocStructInfo* info)
  97. {
  98.   void* next;
  99.   for (; info->firstchunk; info->firstchunk = next) {
  100.     next = *((void**) info->firstchunk);
  101.     XP_FREE(info->firstchunk);
  102.   }
  103.   info->curchunk = NULL;
  104.   info->leftinchunk = 0;
  105.   info->firstfree = NULL;
  106.   info->numalloced = 0;
  107. }
  108.