source: git/omalloc/omBinPage.c @ 1d905d

fieker-DuValspielwiese
Last change on this file since 1d905d was 2c72eb, checked in by Olaf Bachmann <obachman@…>, 23 years ago
* mAlloc.h --> om_Alloc.h git-svn-id: file:///usr/local/Singular/svn/trunk@4881 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 17.5 KB
Line 
1/*******************************************************************
2 *  File:    omBinPage.c
3 *  Purpose: implementation of routines for primitve BinPage managment
4 *  Author:  obachman (Olaf Bachmann)
5 *  Created: 11/99
6 *  Version: $Id: omBinPage.c,v 1.6 2000-12-12 15:26:16 obachman Exp $
7 *******************************************************************/
8#include <limits.h>
9#include "om_Alloc.h"
10#include "omDefaultConfig.h"
11
12/*******************************************************************
13 * 
14 * Local declarations 
15 * 
16 *******************************************************************/
17
18/* define if you want to keep regions approximately in order */
19#define OM_KEEP_REGIONS_ORDER
20
21struct omBinPageRegion_s
22{
23  void* current;        /* linked list of free pages */
24  omBinPageRegion next; /* nex/prev pointer in ring of regions */
25  omBinPageRegion prev;
26  void* init_addr;      /* pointer portion of inital chunk which is still free */
27  void* addr;         /* addr returned by alloc */
28  int   init_pages;   /* number of pages still available in init_chunk */ 
29  int   used_pages;     /* number of used pages */
30  int pages;          /* total size of region */
31};
32
33/* globale variable holding pointing int regions ring */
34static omBinPageRegion om_CurrentBinPageRegion = NULL;
35unsigned long om_MaxBinPageIndex = 0;
36unsigned long om_MinBinPageIndex = ULONG_MAX;
37unsigned long *om_BinPageIndicies = NULL;
38
39/* declaration of local procs */
40static void* omTakeOutConsecutivePages(omBinPageRegion region, int how_many);
41static omBinPageRegion omAllocNewBinPagesRegion(int min_pages);
42static void omFreeBinPagesRegion(omBinPageRegion region);
43
44static void omBinPageIndexFault(unsigned long low_index, unsigned long high_index);
45static void omRegisterBinPages(void* low_addr, int pages);
46static void omUnregisterBinPages(void* low_addr, int pages);
47
48OM_INLINE_LOCAL void omTakeOutRegion(omBinPageRegion region)
49{
50  omAssume(region != NULL);
51 
52  if (region->prev != NULL)
53  {
54    omAssume(region->prev != region && region->prev != region->next);
55    region->prev->next = region->next;
56  }
57 
58  if (region->next != NULL) 
59  {
60    omAssume(region->next != region && region->prev != region->next);
61    region->next->prev = region->prev;
62  }
63}
64
65OM_INLINE_LOCAL void omInsertRegionAfter(omBinPageRegion insert, omBinPageRegion after)
66{
67  omAssume(insert != NULL && after != NULL && insert != after);
68  insert->next = after->next;
69  insert->prev = after;
70  after->next = insert;
71  if (insert->next != NULL)
72  {
73    omAssume(insert->next != insert && insert->next != after);
74    insert->next->prev = insert;
75  }
76}
77
78OM_INLINE_LOCAL void omInsertRegionBefore(omBinPageRegion insert, omBinPageRegion before)
79{
80  omAssume(insert != NULL && before != NULL && insert != before);
81  insert->prev = before->prev;
82  insert->next = before;
83  before->prev = insert;
84  if (insert->prev != NULL)
85    insert->prev->next = insert;
86}
87   
88
89/*******************************************************************
90 * 
91 * Alloc/Free of BinPages
92 * 
93 *******************************************************************/
94#define NEXT_PAGE(page) *((void**) page)
95#define OM_IS_EMPTY_REGION(region) ((region)->current == NULL && (region->init_addr == NULL))
96
97omBinPage omAllocBinPage()
98{
99  omBinPage bin_page;
100
101  if (om_CurrentBinPageRegion == NULL)
102    om_CurrentBinPageRegion = omAllocNewBinPagesRegion(1);
103
104  while (1)
105  {
106    if (om_CurrentBinPageRegion->current != NULL)
107    {
108      bin_page = om_CurrentBinPageRegion->current;
109      om_CurrentBinPageRegion->current = NEXT_PAGE(bin_page);
110      goto Found;
111    }
112    if (om_CurrentBinPageRegion->init_pages)
113    {
114      bin_page = om_CurrentBinPageRegion->init_addr;
115      om_CurrentBinPageRegion->init_pages--;
116      if (om_CurrentBinPageRegion->init_pages) 
117        om_CurrentBinPageRegion->init_addr += SIZEOF_SYSTEM_PAGE;
118      else
119        om_CurrentBinPageRegion->init_addr = NULL;
120      goto Found;
121    }
122    if (om_CurrentBinPageRegion->next != NULL)
123    {
124      om_CurrentBinPageRegion = om_CurrentBinPageRegion->next;
125    }
126    else
127    {
128      omBinPageRegion new_region = omAllocNewBinPagesRegion(1);
129      new_region->prev = om_CurrentBinPageRegion;
130      om_CurrentBinPageRegion->next = new_region;
131      om_CurrentBinPageRegion = new_region;
132    }
133  }
134  while (1);
135       
136  Found:
137  bin_page->region = om_CurrentBinPageRegion;
138  om_CurrentBinPageRegion->used_pages++;
139 
140  om_Info.UsedPages++;
141  om_Info.AvailPages--;
142  if (om_Info.UsedPages > om_Info.MaxPages)
143    om_Info.MaxPages = om_Info.UsedPages;
144 
145  OM_ALLOC_BINPAGE_HOOK;
146  return bin_page;
147}
148
149omBinPage omAllocBinPages(int how_many)
150{
151  omBinPage bin_page;
152  omBinPageRegion region;
153
154  if (om_CurrentBinPageRegion == NULL)
155    om_CurrentBinPageRegion = omAllocNewBinPagesRegion(how_many);
156
157  region = om_CurrentBinPageRegion;
158  while (1)
159  {
160    if (region->init_pages >= how_many)
161    {
162      bin_page = region->init_addr;
163      region->init_pages -= how_many;
164      if (region->init_pages) 
165        region->init_addr += how_many*SIZEOF_SYSTEM_PAGE;
166      else
167        region->init_addr = NULL;
168      goto Found;
169    }
170    if ((bin_page = omTakeOutConsecutivePages(region, how_many)) != NULL)
171    {
172      goto Found;
173    }
174    if (region->next != NULL)
175    {
176      region = region->next;
177    }
178    else
179    {
180      omBinPageRegion new_region = omAllocNewBinPagesRegion(how_many);
181      region->next = new_region;
182      new_region->prev = region;
183      region = new_region;
184    }
185  }
186  while (1);
187       
188  Found:
189  bin_page->region = region;
190  region->used_pages += how_many;
191
192  if (region != om_CurrentBinPageRegion && OM_IS_EMPTY_REGION(region))
193  {
194    omTakeOutRegion(region);
195    omInsertRegionBefore(region, om_CurrentBinPageRegion);
196  }
197  om_Info.UsedPages += how_many;
198  om_Info.AvailPages -= how_many;
199  if (om_Info.UsedPages > om_Info.MaxPages)
200    om_Info.MaxPages = om_Info.UsedPages;
201
202  OM_ALLOC_BINPAGE_HOOK;
203  return bin_page;
204}
205 
206void omFreeBinPages(omBinPage bin_page, int how_many)
207{
208  omBinPageRegion region = bin_page->region;
209
210  region->used_pages -= how_many;
211  if (region->used_pages == 0)
212  {
213    if (region == om_CurrentBinPageRegion) 
214    {
215      if (region->next != NULL) 
216        om_CurrentBinPageRegion = region->next;
217      else
218        om_CurrentBinPageRegion = region->prev;
219    }
220    omTakeOutRegion(region);
221    omFreeBinPagesRegion(region);
222  }
223  else
224  {
225    if (region != om_CurrentBinPageRegion && OM_IS_EMPTY_REGION(region))
226    {
227      omTakeOutRegion(region);
228      omInsertRegionAfter(region, om_CurrentBinPageRegion);
229    }
230    if (how_many > 1)
231    {
232      int i = how_many;
233      void* page = bin_page;
234     
235      while (i > 1)
236      {
237        NEXT_PAGE(page) = page + SIZEOF_SYSTEM_PAGE;
238        page = NEXT_PAGE(page);
239        i--;
240      }
241      NEXT_PAGE(page) = region->current;
242    }
243    else
244    {
245      NEXT_PAGE(bin_page) = region->current;
246    }
247    region->current = (void*) bin_page;
248  }
249  om_Info.AvailPages += how_many;
250  om_Info.UsedPages -= how_many;
251  OM_FREE_BINPAGE_HOOK;
252}
253 
254static void* omTakeOutConsecutivePages(omBinPageRegion region, int pages)
255{
256  void* current;
257  void* iter;
258  void* prev = NULL;
259  void* bin_page;
260  int found;
261  current = region->current;
262  while (current != NULL)
263  {
264    found = 1;
265    iter = current;
266    while (NEXT_PAGE(iter) == iter + SIZEOF_SYSTEM_PAGE)
267    {
268      iter = NEXT_PAGE(iter);
269      /* handle pathological case that iter + SIZEOF_SYSTEM_PAGE == 0 */
270      if (iter == NULL) return NULL;
271      found++;
272      if (found == pages)
273      {
274        bin_page = current;
275        if (current == region->current)
276        {
277          region->current = NEXT_PAGE(iter);
278        }
279        else
280        {
281          omAssume(prev != NULL);
282          NEXT_PAGE(prev) = NEXT_PAGE(iter);
283        }
284        return bin_page;
285      }
286    }
287    prev = iter;
288    current = NEXT_PAGE(iter);
289  }
290  return NULL;
291}
292
293/* Alloc a new region and insert into regions ring, set current to new region */
294static omBinPageRegion omAllocNewBinPagesRegion(int min_pages)
295{
296  omBinPageRegion region = omAllocFromSystem(sizeof(omBinPageRegion_t));
297  void* addr;
298  int pages = (min_pages>om_Opts.PagesPerRegion ? min_pages : om_Opts.PagesPerRegion);
299  size_t size = pages*SIZEOF_SYSTEM_PAGE;
300 
301  addr = _omVallocFromSystem(size, 1);
302  if (addr == NULL) 
303  {
304    pages = min_pages;
305    size = min_pages*SIZEOF_SYSTEM_PAGE;
306    addr = omVallocFromSystem(size);
307  }
308 
309  omRegisterBinPages(addr, pages);
310  region->addr = addr;
311  region->pages = pages;
312  region->used_pages = 0;
313  region->init_addr = addr;
314  region->init_pages = pages;
315  region->current = NULL;
316  region->next = NULL;
317  region->prev = NULL;
318
319  om_Info.AvailPages += pages;
320
321  om_Info.CurrentRegionsAlloc++;
322  if (om_Info.CurrentRegionsAlloc > om_Info.MaxRegionsAlloc)
323    om_Info.MaxRegionsAlloc = om_Info.CurrentRegionsAlloc;
324
325  return region;
326}
327
328/* Free region */
329static void omFreeBinPagesRegion(omBinPageRegion region)
330{
331  omAssume(region != NULL && region->used_pages == 0);
332
333  om_Info.AvailPages -= region->pages;
334  om_Info.CurrentRegionsAlloc--;
335
336  omUnregisterBinPages(region->addr, region->pages);
337  omVfreeToSystem(region->addr, region->pages*SIZEOF_SYSTEM_PAGE);
338  omFreeSizeToSystem(region, sizeof(omBinPageRegion_t));
339}
340
341/*******************************************************************
342 * 
343 * BinPage registration
344 * 
345 *******************************************************************/
346
347static void omBinPageIndexFault(unsigned long low_index, unsigned long high_index)
348{
349  unsigned long index_diff = high_index - low_index;
350  long i;
351  omAssume(low_index <= high_index &&
352           (high_index > om_MaxBinPageIndex || low_index < om_MinBinPageIndex));
353 
354  if (om_BinPageIndicies == NULL)
355  {
356    om_BinPageIndicies = (unsigned long*) omAllocFromSystem((index_diff + 1)*SIZEOF_LONG);
357    om_MaxBinPageIndex = high_index;
358    om_MinBinPageIndex = low_index;
359    for (i=0; i<=index_diff; i++) om_BinPageIndicies[i] = 0;
360  }
361  else
362  {
363    unsigned long old_length = om_MaxBinPageIndex - om_MinBinPageIndex + 1;
364    unsigned long new_length = (low_index < om_MinBinPageIndex ? 
365                                om_MaxBinPageIndex - low_index : 
366                                high_index - om_MinBinPageIndex) + 1;
367    om_BinPageIndicies  = (unsigned long*) omReallocSizeFromSystem(om_BinPageIndicies, old_length*SIZEOF_LONG,
368                                                                   new_length*SIZEOF_LONG);
369    if (low_index < om_MinBinPageIndex)
370    {
371      unsigned long offset = new_length - old_length;
372      for (i=old_length - 1; i >= 0; i--) om_BinPageIndicies[i+offset] = om_BinPageIndicies[i];
373      for (i=0; i<offset; i++)  om_BinPageIndicies[i] = 0;
374      om_MinBinPageIndex = low_index;
375    }
376    else
377    {
378      for (i=old_length; i<new_length; i++) om_BinPageIndicies[i] = 0;
379      om_MaxBinPageIndex = high_index;
380    }
381  }
382}
383
384static void omRegisterBinPages(void* low_addr, int pages)
385{
386  unsigned long low_index = omGetPageIndexOfAddr(low_addr);
387  void* high_addr = low_addr + (pages-1)*SIZEOF_SYSTEM_PAGE;
388  unsigned long high_index = omGetPageIndexOfAddr(high_addr);
389  unsigned long shift;
390   
391  if (low_index < om_MinBinPageIndex || high_index > om_MaxBinPageIndex)
392    omBinPageIndexFault(low_index, high_index);
393
394  shift = omGetPageShiftOfAddr(low_addr);
395  if (low_index < high_index)
396  {
397    if (shift == 0)
398    {
399      om_BinPageIndicies[low_index-om_MinBinPageIndex] = ULONG_MAX;
400    }
401    else
402    {
403      om_BinPageIndicies[low_index-om_MinBinPageIndex] |= ~ ((((unsigned long) 1) << shift) - 1);
404    }
405    for (shift = low_index+1; shift < high_index; shift++)
406    {
407      om_BinPageIndicies[shift-om_MinBinPageIndex] = ULONG_MAX;
408    }
409    shift = omGetPageShiftOfAddr(high_addr);
410    if (shift == BIT_SIZEOF_LONG - 1)
411    {
412      om_BinPageIndicies[high_index - om_MinBinPageIndex] = ULONG_MAX;
413    }
414    else
415    {
416      om_BinPageIndicies[high_index-om_MinBinPageIndex] |= ((((unsigned long) 1) << (shift + 1)) - 1);
417    }
418  }
419  else
420  {
421    high_index = omGetPageShiftOfAddr(high_addr);
422    while (high_index > shift)
423    {
424      om_BinPageIndicies[low_index-om_MinBinPageIndex] |= (((unsigned long) 1) << high_index);
425      high_index--;
426    }
427    om_BinPageIndicies[low_index-om_MinBinPageIndex] |= (((unsigned long) 1) << shift);
428  }
429}
430
431static void omUnregisterBinPages(void* low_addr, int pages)
432{
433  unsigned long low_index = omGetPageIndexOfAddr(low_addr);
434  void* high_addr = low_addr + (pages-1)*SIZEOF_SYSTEM_PAGE;
435  unsigned long high_index = omGetPageIndexOfAddr(high_addr);
436  unsigned long shift;
437 
438  shift = omGetPageShiftOfAddr(low_addr);
439  if (low_index < high_index)
440  {
441    if (shift == 0)
442    {
443      om_BinPageIndicies[low_index-om_MinBinPageIndex] = 0;
444    }
445    else
446    {
447      om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ((((unsigned long) 1) << shift) - 1);
448    }
449    for (shift = low_index+1; shift < high_index; shift++)
450    {
451      om_BinPageIndicies[shift-om_MinBinPageIndex] = 0;
452    }
453    shift = omGetPageShiftOfAddr(high_addr);
454    if (shift == BIT_SIZEOF_LONG - 1)
455    {
456      om_BinPageIndicies[high_index - om_MinBinPageIndex] = 0;
457    }
458    else
459    {
460      om_BinPageIndicies[high_index-om_MinBinPageIndex] &= ~ ((((unsigned long) 1) << (shift + 1)) - 1);
461    }
462  }
463  else
464  {
465     high_index = omGetPageShiftOfAddr(high_addr);
466     while (high_index > shift)
467     {
468       om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ~(((unsigned long) 1) << high_index);
469       high_index--;
470     }
471     om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ~(((unsigned long) 1) << shift);
472  }
473}
474   
475/***********************************************************************
476 *
477 * checking routines
478 *
479 *******************************************************************/
480#ifndef OM_NDEBUG
481#include "omDebug.h"
482
483int omIsKnownMemoryRegion(omBinPageRegion region)
484{
485  omBinPageRegion iter = om_CurrentBinPageRegion;
486 
487  if (region == NULL || iter == NULL) return 0;
488  iter = omGListLast(om_CurrentBinPageRegion, prev);
489  do
490  {
491    if (region == iter) return 1;
492    iter = iter->next;
493  }
494  while (iter != NULL);
495  return 0;
496}
497 
498
499omError_t omCheckBinPageRegion(omBinPageRegion region, int level, omError_t report, OM_FLR_DECL)
500{
501  if (level <= 0) return omError_NoError;
502 
503  omCheckReturn(omCheckPtr(region, report, OM_FLR_VAL));
504  omCheckReturnCorrupted(! omIsKnownMemoryRegion(region));
505  omCheckReturnCorrupted(! omIsAddrPageAligned(region->addr) || ! omIsAddrPageAligned(region->current));
506  omCheckReturnCorrupted(region->used_pages < 0);
507  omCheckReturnCorrupted(region->init_pages < 0 || region->init_pages > region->pages);
508 
509  if (region->init_pages)
510  {
511    omCheckReturnCorrupted(! omIsAddrPageAligned(region->init_addr));
512    omCheckReturnCorrupted(! (region->init_addr >= region->addr
513                              && region->init_addr <= region->addr + (region->pages -1)*SIZEOF_SYSTEM_PAGE));
514    omCheckReturnCorrupted(region->init_addr != 
515                           region->addr + (region->pages - region->init_pages)*SIZEOF_SYSTEM_PAGE);
516  }
517   
518  omCheckReturn(omCheckList(region->current, level, report, OM_FLR_VAL));
519  omCheckReturnCorrupted(region->current == NULL && region->used_pages + region->init_pages != region->pages);
520  omCheckReturnCorrupted(level > 1 && 
521                         omListLength(region->current)+region->used_pages+region->init_pages != region->pages);
522  return omError_NoError;
523}
524
525omError_t omCheckBinPageRegions(int level, omError_t report, OM_FLR_DECL)
526{
527  omBinPageRegion iter = om_CurrentBinPageRegion;
528
529  if (level <= 0) return omError_NoError;
530  if (iter == NULL) return omError_NoError;
531
532  omCheckReturnError(om_CurrentBinPageRegion->next != NULL && OM_IS_EMPTY_REGION(om_CurrentBinPageRegion->next),
533                     omError_InternalBug);
534  omCheckReturnError(om_CurrentBinPageRegion->prev != NULL && ! OM_IS_EMPTY_REGION(om_CurrentBinPageRegion->prev),
535                     omError_InternalBug);
536
537
538  if (level > 1)
539  {
540    omBinPageRegion prev_last = (omBinPageRegion) omGListLast(om_CurrentBinPageRegion, prev);
541    omBinPageRegion next_last = (omBinPageRegion) omGListLast(om_CurrentBinPageRegion, next);
542   
543    omCheckReturn(omCheckGList(iter, next, level, report, OM_FLR_VAL));
544    omCheckReturn(omCheckGList(iter, prev, level, report, OM_FLR_VAL));
545
546    omCheckReturnCorrupted(omGListLength(prev_last, next)
547                          != 
548                          omGListLength(next_last, prev));
549   
550    omCheckReturn(omCheckBinPageRegion(om_CurrentBinPageRegion, level - 1, report, OM_FLR_VAL));
551
552    iter = om_CurrentBinPageRegion->next;
553    while (iter)
554    {
555      omCheckReturnError(OM_IS_EMPTY_REGION(iter), omError_InternalBug);
556
557      omCheckReturn(omCheckBinPageRegion(iter, level - 1, report, OM_FLR_VAL));
558      iter = iter->next;
559    }
560
561    iter = om_CurrentBinPageRegion->prev;
562    while (iter)
563    {
564      omCheckReturnError( !OM_IS_EMPTY_REGION(iter), omError_InternalBug);
565      omCheckReturn(omCheckBinPageRegion(iter, level - 1, report, OM_FLR_VAL));
566      iter = iter->prev;
567    }
568  }
569  return omError_NoError;
570}
571
572omBinPageRegion omFindRegionOfAddr(void* addr)
573{
574  omBinPageRegion region = om_CurrentBinPageRegion;
575 
576  if (region == NULL) return 0;
577  region = omGListLast(region, prev);
578  do
579  {
580    if (addr >= region->addr && addr < region->addr + (region->pages)*SIZEOF_SYSTEM_PAGE)
581      return region;
582    region = region->next;
583  }
584  while (region != NULL);
585  return NULL;
586}
587
588int omIsAddrOnFreeBinPage(void* addr)
589{
590  omBinPageRegion region = om_CurrentBinPageRegion;
591 
592  if (region == NULL) return 0;
593  do
594  {
595    if (addr > region->addr && addr < region->addr + (region->pages)*SIZEOF_SYSTEM_PAGE)
596    {
597      if (omIsOnList(region->current, omGetPageOfAddr(addr))) return 1;
598      return 0;
599    }
600    region = region->next;
601  }
602  while (region != NULL);
603  return 0;
604}
605 
606#endif /* ! OM_NDEBUG */
Note: See TracBrowser for help on using the repository browser.