home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / layout / layarena.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.3 KB  |  139 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. #include "xp.h"
  20. #include "layout.h"
  21.  
  22. #ifdef MEMORY_ARENAS
  23.  
  24.  
  25. #define ARENA_SIZE    16384
  26.  
  27.  
  28. void
  29. lo_InitializeMemoryArena(lo_TopState *top_state)
  30. {
  31.     lo_arena *aptr;
  32.     int32 arena_size;
  33.  
  34.     top_state->first_arena = NULL;
  35.     top_state->current_arena = NULL;
  36.     arena_size = (int32) (ARENA_SIZE + sizeof(lo_arena));
  37.     arena_size = ((arena_size + 3) & ~3);
  38.     aptr = (lo_arena *)XP_ALLOC(arena_size);
  39.     if (aptr == NULL)
  40.     {
  41.         top_state->first_arena = NULL;
  42.         top_state->current_arena = NULL;
  43.         return;
  44.     }
  45.  
  46. #ifdef XP_WIN16
  47.     aptr->limit = (char *)((int32)((char *)aptr) + arena_size);
  48.     aptr->avail = (char *)((int32)((char *)aptr) + (int32)sizeof(lo_arena));
  49. #else
  50.     aptr->limit = (char *)aptr + arena_size;
  51.     aptr->avail = (char *)aptr + sizeof(lo_arena);
  52. #endif
  53.     aptr->next = NULL;
  54.     top_state->first_arena = aptr;
  55.     top_state->current_arena = aptr;
  56. }
  57.  
  58.  
  59. int32
  60. lo_FreeMemoryArena(lo_arena *arena)
  61. {
  62.     int32 cnt;
  63.  
  64.     cnt = 0;
  65.     while (arena != NULL)
  66.     {
  67.         lo_arena *tmp_aptr;
  68.  
  69.         tmp_aptr = arena;
  70.         arena = arena->next;
  71.         cnt += (int32 )(tmp_aptr->limit - (char *)tmp_aptr);
  72.         XP_FREE((char *)tmp_aptr);
  73.     }
  74.     return(cnt);
  75. }
  76.  
  77.  
  78. char *
  79. lo_MemoryArenaAllocate(lo_TopState *top_state, int32 size)
  80. {
  81.     lo_arena *aptr;
  82.     char *ptr;
  83.  
  84.     if ((top_state == NULL)||(top_state->current_arena == NULL))
  85.     {
  86.         return(NULL);
  87.     }
  88.  
  89.     aptr = top_state->current_arena;
  90. #ifdef XP_WIN16
  91.     while ((int32)((int32)((char *)aptr->avail) + size) >
  92.         (int32)((char *)aptr->limit))
  93. #else
  94.     while ((aptr->avail + size) > aptr->limit)
  95. #endif
  96.     {
  97.         if (aptr->next != NULL)
  98.         {
  99.             aptr = aptr->next;
  100. #ifdef XP_WIN16
  101.             aptr->avail = (char *)((int32)((char *)aptr) + (int32)sizeof(lo_arena));
  102. #else
  103.             aptr->avail = (char *)aptr + sizeof(lo_arena);
  104. #endif
  105.         }
  106.         else
  107.         {
  108.             int32 arena_size;
  109.  
  110.             arena_size = (int32) (ARENA_SIZE + sizeof(lo_arena));
  111.             arena_size = ((arena_size + 3) & ~3);
  112.             aptr->next = (lo_arena *)XP_ALLOC(arena_size);
  113.             if (aptr->next == NULL)
  114.             {
  115.                 return(NULL);
  116.             }
  117.             aptr = aptr->next;
  118. #ifdef XP_WIN16
  119.             aptr->limit = (char *)((int32)((char *)aptr) + arena_size);
  120.             aptr->avail = (char *)((int32)((char *)aptr) + (int32)sizeof(lo_arena));
  121. #else
  122.             aptr->limit = (char *)aptr + arena_size;
  123.             aptr->avail = (char *)aptr + sizeof(lo_arena);
  124. #endif
  125.             aptr->next = NULL;
  126.         }
  127.     }
  128.     top_state->current_arena = aptr;
  129.     ptr = (char *)aptr->avail;
  130. #ifdef XP_WIN16
  131.     aptr->avail = (char *)((int32)((char *)aptr->avail) + size);
  132. #else
  133.     aptr->avail += size;
  134. #endif
  135.     return(ptr);
  136. }
  137.  
  138. #endif /* MEMORY_ARENAS */
  139.