File: omBinPage.c

package info (click to toggle)
singular 1%3A4.0.3-p3%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 33,040 kB
  • ctags: 19,347
  • sloc: cpp: 271,589; ansic: 13,500; lisp: 4,242; yacc: 1,656; lex: 1,377; makefile: 1,264; perl: 632; sh: 467; python: 233; xml: 182
file content (606 lines) | stat: -rw-r--r-- 17,883 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*******************************************************************
 *  File:    omBinPage.c
 *  Purpose: implementation of routines for primitve BinPage managment
 *  Author:  obachman (Olaf Bachmann)
 *  Created: 11/99
 *******************************************************************/
#include <mylimits.h>
#include "omalloc.h"
#include "omDefaultConfig.h"

/*******************************************************************
 *
 * Local declarations
 *
 *******************************************************************/

/* define if you want to keep regions approximately in order */
#define OM_KEEP_REGIONS_ORDER

struct omBinPageRegion_s
{
  void* current;        /* linked list of free pages */
  omBinPageRegion next; /* nex/prev pointer in ring of regions */
  omBinPageRegion prev;
  char* init_addr;      /* pointer portion of inital chunk which is still free */
  char* addr;         /* addr returned by alloc */
  int   init_pages;   /* number of pages still available in init_chunk */
  int   used_pages;     /* number of used pages */
  int pages;          /* total size of region */
};

/* globale variable holding pointing int regions ring */
static omBinPageRegion om_CurrentBinPageRegion = NULL;
unsigned long om_MaxBinPageIndex = 0;
unsigned long om_MinBinPageIndex = ULONG_MAX;
unsigned long *om_BinPageIndicies = NULL;

/* declaration of local procs */
static void* omTakeOutConsecutivePages(omBinPageRegion region, int how_many);
static omBinPageRegion omAllocNewBinPagesRegion(int min_pages);
static void omFreeBinPagesRegion(omBinPageRegion region);

static void omBinPageIndexFault(unsigned long low_index, unsigned long high_index);
static void omRegisterBinPages(void* low_addr, int pages);
static void omUnregisterBinPages(void* low_addr, int pages);

OM_INLINE_LOCAL void omTakeOutRegion(omBinPageRegion region)
{
  omAssume(region != NULL);

  if (region->prev != NULL)
  {
    omAssume(region->prev != region && region->prev != region->next);
    region->prev->next = region->next;
  }

  if (region->next != NULL)
  {
    omAssume(region->next != region && region->prev != region->next);
    region->next->prev = region->prev;
  }
}

OM_INLINE_LOCAL void omInsertRegionAfter(omBinPageRegion insert, omBinPageRegion after)
{
  omAssume(insert != NULL && after != NULL && insert != after);
  insert->next = after->next;
  insert->prev = after;
  after->next = insert;
  if (insert->next != NULL)
  {
    omAssume(insert->next != insert && insert->next != after);
    insert->next->prev = insert;
  }
}

OM_INLINE_LOCAL void omInsertRegionBefore(omBinPageRegion insert, omBinPageRegion before)
{
  omAssume(insert != NULL && before != NULL && insert != before);
  insert->prev = before->prev;
  insert->next = before;
  before->prev = insert;
  if (insert->prev != NULL)
    insert->prev->next = insert;
}


/*******************************************************************
 *
 * Alloc/Free of BinPages
 *
 *******************************************************************/
#define NEXT_PAGE(page) *((void**) page)
#define OM_IS_EMPTY_REGION(region) ((region)->current == NULL && (region->init_addr == NULL))

omBinPage omAllocBinPage()
{
  omBinPage bin_page;

  if (om_CurrentBinPageRegion == NULL)
    om_CurrentBinPageRegion = omAllocNewBinPagesRegion(1);

  while (1)
  {
    if (om_CurrentBinPageRegion->current != NULL)
    {
      bin_page = om_CurrentBinPageRegion->current;
      om_CurrentBinPageRegion->current = NEXT_PAGE(bin_page);
      goto Found;
    }
    if (om_CurrentBinPageRegion->init_pages > 0)
    {
      bin_page = (omBinPage)om_CurrentBinPageRegion->init_addr;
      om_CurrentBinPageRegion->init_pages--;
      if (om_CurrentBinPageRegion->init_pages > 0)
        om_CurrentBinPageRegion->init_addr += SIZEOF_SYSTEM_PAGE;
      else
        om_CurrentBinPageRegion->init_addr = NULL;
      goto Found;
    }
    if (om_CurrentBinPageRegion->next != NULL)
    {
      om_CurrentBinPageRegion = om_CurrentBinPageRegion->next;
    }
    else
    {
      omBinPageRegion new_region = omAllocNewBinPagesRegion(1);
      new_region->prev = om_CurrentBinPageRegion;
      om_CurrentBinPageRegion->next = new_region;
      om_CurrentBinPageRegion = new_region;
    }
  }

  Found:
  bin_page->region = om_CurrentBinPageRegion;
  om_CurrentBinPageRegion->used_pages++;

  om_Info.UsedPages++;
  om_Info.AvailPages--;
  if (om_Info.UsedPages > om_Info.MaxPages)
    om_Info.MaxPages = om_Info.UsedPages;

  OM_ALLOC_BINPAGE_HOOK;
  return bin_page;
}

omBinPage omAllocBinPages(int how_many)
{
  omBinPage bin_page;
  omBinPageRegion region;

  if (om_CurrentBinPageRegion == NULL)
    om_CurrentBinPageRegion = omAllocNewBinPagesRegion(how_many);

  region = om_CurrentBinPageRegion;
  while (1)
  {
    if (region->init_pages >= how_many)
    {
      bin_page = (omBinPage)region->init_addr;
      region->init_pages -= how_many;
      if (region->init_pages)
        region->init_addr += how_many*SIZEOF_SYSTEM_PAGE;
      else
        region->init_addr = NULL;
      goto Found;
    }
    if ((bin_page = omTakeOutConsecutivePages(region, how_many)) != NULL)
    {
      goto Found;
    }
    if (region->next != NULL)
    {
      region = region->next;
    }
    else
    {
      omBinPageRegion new_region = omAllocNewBinPagesRegion(how_many);
      region->next = new_region;
      new_region->prev = region;
      region = new_region;
    }
  }
  /*while (1) */

  Found:
  bin_page->region = region;
  region->used_pages += how_many;

  if (region != om_CurrentBinPageRegion && OM_IS_EMPTY_REGION(region))
  {
    omTakeOutRegion(region);
    omInsertRegionBefore(region, om_CurrentBinPageRegion);
  }
  om_Info.UsedPages += how_many;
  om_Info.AvailPages -= how_many;
  if (om_Info.UsedPages > om_Info.MaxPages)
    om_Info.MaxPages = om_Info.UsedPages;

  OM_ALLOC_BINPAGE_HOOK;
  return bin_page;
}

void omFreeBinPages(omBinPage bin_page, int how_many)
{
  omBinPageRegion region = bin_page->region;

  region->used_pages -= how_many;
  if (region->used_pages == 0)
  {
    if (region == om_CurrentBinPageRegion)
    {
      if (region->next != NULL)
        om_CurrentBinPageRegion = region->next;
      else
        om_CurrentBinPageRegion = region->prev;
    }
    omTakeOutRegion(region);
    omFreeBinPagesRegion(region);
  }
  else
  {
    if (region != om_CurrentBinPageRegion && OM_IS_EMPTY_REGION(region))
    {
      omTakeOutRegion(region);
      omInsertRegionAfter(region, om_CurrentBinPageRegion);
    }
    if (how_many > 1)
    {
      int i = how_many;
      char* page = (char *)bin_page;

      while (i > 1)
      {
        NEXT_PAGE(page) = page + SIZEOF_SYSTEM_PAGE;
        page = NEXT_PAGE(page);
        i--;
      }
      NEXT_PAGE(page) = region->current;
    }
    else
    {
      NEXT_PAGE(bin_page) = region->current;
    }
    region->current = (void*) bin_page;
  }
  om_Info.AvailPages += how_many;
  om_Info.UsedPages -= how_many;
  OM_FREE_BINPAGE_HOOK;
}

static void* omTakeOutConsecutivePages(omBinPageRegion region, int pages)
{
  void* current;
  char* iter;
  void* prev = NULL;
  void* bin_page;
  int found;
  current = region->current;
  while (current != NULL)
  {
    found = 1;
    iter = current;
    while (NEXT_PAGE(iter) == (iter + SIZEOF_SYSTEM_PAGE))
    {
      iter = NEXT_PAGE(iter);
      /* handle pathological case that iter + SIZEOF_SYSTEM_PAGE == 0 */
      if (iter == NULL) return NULL;
      found++;
      if (found == pages)
      {
        bin_page = current;
        if (current == region->current)
        {
          region->current = NEXT_PAGE(iter);
        }
        else
        {
          omAssume(prev != NULL);
          NEXT_PAGE(prev) = NEXT_PAGE(iter);
        }
        return bin_page;
      }
    }
    prev = iter;
    current = NEXT_PAGE(iter);
  }
  return NULL;
}

/* Alloc a new region and insert into regions ring, set current to new region */
static omBinPageRegion omAllocNewBinPagesRegion(int min_pages)
{
  omBinPageRegion region = omAllocFromSystem(sizeof(omBinPageRegion_t));
  void* addr;
  int pages = (min_pages>om_Opts.PagesPerRegion ? min_pages : om_Opts.PagesPerRegion);
  size_t size = pages*SIZEOF_SYSTEM_PAGE;

  addr = _omVallocFromSystem(size, 1);
  if (addr == NULL)
  {
    pages = min_pages;
    size = min_pages*SIZEOF_SYSTEM_PAGE;
    addr = omVallocFromSystem(size);
  }

  omRegisterBinPages(addr, pages);
  region->addr = addr;
  region->pages = pages;
  region->used_pages = 0;
  region->init_addr = addr;
  region->init_pages = pages;
  region->current = NULL;
  region->next = NULL;
  region->prev = NULL;

  om_Info.AvailPages += pages;

  om_Info.CurrentRegionsAlloc++;
  if (om_Info.CurrentRegionsAlloc > om_Info.MaxRegionsAlloc)
    om_Info.MaxRegionsAlloc = om_Info.CurrentRegionsAlloc;

  return region;
}

/* Free region */
static void omFreeBinPagesRegion(omBinPageRegion region)
{
  omAssume(region != NULL && region->used_pages == 0);

  om_Info.AvailPages -= region->pages;
  om_Info.CurrentRegionsAlloc--;

  omUnregisterBinPages(region->addr, region->pages);
  omVfreeToSystem(region->addr, region->pages*SIZEOF_SYSTEM_PAGE);
  omFreeSizeToSystem(region, sizeof(omBinPageRegion_t));
}

/*******************************************************************
 *
 * BinPage registration
 *
 *******************************************************************/

static void omBinPageIndexFault(unsigned long low_index, unsigned long high_index)
{
  unsigned long index_diff = high_index - low_index;
  long i;
  omAssume(low_index <= high_index &&
           (high_index > om_MaxBinPageIndex || low_index < om_MinBinPageIndex));

  if (om_BinPageIndicies == NULL)
  {
    om_BinPageIndicies = (unsigned long*) omAllocFromSystem((index_diff + 1)*SIZEOF_LONG);
    om_MaxBinPageIndex = high_index;
    om_MinBinPageIndex = low_index;
    for (i=0; i<=index_diff; i++) om_BinPageIndicies[i] = 0;
  }
  else
  {
    unsigned long old_length = om_MaxBinPageIndex - om_MinBinPageIndex + 1;
    unsigned long new_length = (low_index < om_MinBinPageIndex ?
                                om_MaxBinPageIndex - low_index :
                                high_index - om_MinBinPageIndex) + 1;
    om_BinPageIndicies  = (unsigned long*) omReallocSizeFromSystem(om_BinPageIndicies, old_length*SIZEOF_LONG,
                                                                   new_length*SIZEOF_LONG);
    if (low_index < om_MinBinPageIndex)
    {
      unsigned long offset = new_length - old_length;
      for (i=old_length - 1; i >= 0; i--) om_BinPageIndicies[i+offset] = om_BinPageIndicies[i];
      for (i=0; i<offset; i++)  om_BinPageIndicies[i] = 0;
      om_MinBinPageIndex = low_index;
    }
    else
    {
      for (i=old_length; i<new_length; i++) om_BinPageIndicies[i] = 0;
      om_MaxBinPageIndex = high_index;
    }
  }
}

static void omRegisterBinPages(void* low_addr, int pages)
{
  unsigned long low_index = omGetPageIndexOfAddr(low_addr);
  char* high_addr = (char *)low_addr + (pages-1)*SIZEOF_SYSTEM_PAGE;
  unsigned long high_index = omGetPageIndexOfAddr(high_addr);
  unsigned long shift;

  if (low_index < om_MinBinPageIndex || high_index > om_MaxBinPageIndex)
    omBinPageIndexFault(low_index, high_index);

  shift = omGetPageShiftOfAddr(low_addr);
  if (low_index < high_index)
  {
    if (shift == 0)
    {
      om_BinPageIndicies[low_index-om_MinBinPageIndex] = ULONG_MAX;
    }
    else
    {
      om_BinPageIndicies[low_index-om_MinBinPageIndex] |= ~ ((((unsigned long) 1) << shift) - 1);
    }
    for (shift = low_index+1; shift < high_index; shift++)
    {
      om_BinPageIndicies[shift-om_MinBinPageIndex] = ULONG_MAX;
    }
    shift = omGetPageShiftOfAddr(high_addr);
    if (shift == BIT_SIZEOF_LONG - 1)
    {
      om_BinPageIndicies[high_index - om_MinBinPageIndex] = ULONG_MAX;
    }
    else
    {
      om_BinPageIndicies[high_index-om_MinBinPageIndex] |= ((((unsigned long) 1) << (shift + 1)) - 1);
    }
  }
  else
  {
    high_index = omGetPageShiftOfAddr(high_addr);
    while (high_index > shift)
    {
      om_BinPageIndicies[low_index-om_MinBinPageIndex] |= (((unsigned long) 1) << high_index);
      high_index--;
    }
    om_BinPageIndicies[low_index-om_MinBinPageIndex] |= (((unsigned long) 1) << shift);
  }
}

static void omUnregisterBinPages(void* low_addr, int pages)
{
  unsigned long low_index = omGetPageIndexOfAddr(low_addr);
  char* high_addr = (char *)low_addr + (pages-1)*SIZEOF_SYSTEM_PAGE;
  unsigned long high_index = omGetPageIndexOfAddr(high_addr);
  unsigned long shift;

  shift = omGetPageShiftOfAddr(low_addr);
  if (low_index < high_index)
  {
    if (shift == 0)
    {
      om_BinPageIndicies[low_index-om_MinBinPageIndex] = 0;
    }
    else
    {
      om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ((((unsigned long) 1) << shift) - 1);
    }
    for (shift = low_index+1; shift < high_index; shift++)
    {
      om_BinPageIndicies[shift-om_MinBinPageIndex] = 0;
    }
    shift = omGetPageShiftOfAddr(high_addr);
    if (shift == BIT_SIZEOF_LONG - 1)
    {
      om_BinPageIndicies[high_index - om_MinBinPageIndex] = 0;
    }
    else
    {
      om_BinPageIndicies[high_index-om_MinBinPageIndex] &= ~ ((((unsigned long) 1) << (shift + 1)) - 1);
    }
  }
  else
  {
     high_index = omGetPageShiftOfAddr(high_addr);
     while (high_index > shift)
     {
       om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ~(((unsigned long) 1) << high_index);
       high_index--;
     }
     om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ~(((unsigned long) 1) << shift);
  }
}

/***********************************************************************
 *
 * checking routines
 *
 *******************************************************************/
#ifndef OM_NDEBUG
#include "omDebug.h"

int omIsKnownMemoryRegion(omBinPageRegion region)
{
  omBinPageRegion iter = om_CurrentBinPageRegion;

  if (region == NULL || iter == NULL) return 0;
  iter = omGListLast(om_CurrentBinPageRegion, prev);
  do
  {
    if (region == iter) return 1;
    iter = iter->next;
  }
  while (iter != NULL);
  return 0;
}


omError_t omCheckBinPageRegion(omBinPageRegion region, int level, omError_t report, OM_FLR_DECL)
{
  if (level <= 0) return omError_NoError;

  omCheckReturn(omCheckPtr(region, report, OM_FLR_VAL));
  omCheckReturnCorrupted(! omIsKnownMemoryRegion(region));
  omCheckReturnCorrupted(! omIsAddrPageAligned(region->addr) || ! omIsAddrPageAligned(region->current));
  omCheckReturnCorrupted(region->used_pages < 0);
  omCheckReturnCorrupted(region->init_pages < 0 || region->init_pages > region->pages);

  if (region->init_pages)
  {
    omCheckReturnCorrupted(! omIsAddrPageAligned(region->init_addr));
    omCheckReturnCorrupted(! (region->init_addr >= region->addr
                              && region->init_addr <= region->addr + (region->pages -1)*SIZEOF_SYSTEM_PAGE));
    omCheckReturnCorrupted(region->init_addr !=
                           region->addr + (region->pages - region->init_pages)*SIZEOF_SYSTEM_PAGE);
  }

  omCheckReturn(omCheckList(region->current, level, report, OM_FLR_VAL));
  omCheckReturnCorrupted(region->current == NULL && region->used_pages + region->init_pages != region->pages);
  omCheckReturnCorrupted(level > 1 &&
                         omListLength(region->current)+region->used_pages+region->init_pages != region->pages);
  return omError_NoError;
}

omError_t omCheckBinPageRegions(int level, omError_t report, OM_FLR_DECL)
{
  omBinPageRegion iter = om_CurrentBinPageRegion;

  if (level <= 0) return omError_NoError;
  if (iter == NULL) return omError_NoError;

  omCheckReturnError(om_CurrentBinPageRegion->next != NULL && OM_IS_EMPTY_REGION(om_CurrentBinPageRegion->next),
                     omError_InternalBug);
  omCheckReturnError(om_CurrentBinPageRegion->prev != NULL && ! OM_IS_EMPTY_REGION(om_CurrentBinPageRegion->prev),
                     omError_InternalBug);


  if (level > 1)
  {
    omBinPageRegion prev_last = (omBinPageRegion) omGListLast(om_CurrentBinPageRegion, prev);
    omBinPageRegion next_last = (omBinPageRegion) omGListLast(om_CurrentBinPageRegion, next);

    omCheckReturn(omCheckGList(iter, next, level, report, OM_FLR_VAL));
    omCheckReturn(omCheckGList(iter, prev, level, report, OM_FLR_VAL));

    omCheckReturnCorrupted(omGListLength(prev_last, next)
                          !=
                          omGListLength(next_last, prev));

    omCheckReturn(omCheckBinPageRegion(om_CurrentBinPageRegion, level - 1, report, OM_FLR_VAL));

    iter = om_CurrentBinPageRegion->next;
    while (iter)
    {
      omCheckReturnError(OM_IS_EMPTY_REGION(iter), omError_InternalBug);

      omCheckReturn(omCheckBinPageRegion(iter, level - 1, report, OM_FLR_VAL));
      iter = iter->next;
    }

    iter = om_CurrentBinPageRegion->prev;
    while (iter)
    {
      omCheckReturnError( !OM_IS_EMPTY_REGION(iter), omError_InternalBug);
      omCheckReturn(omCheckBinPageRegion(iter, level - 1, report, OM_FLR_VAL));
      iter = iter->prev;
    }
  }
  return omError_NoError;
}

omBinPageRegion omFindRegionOfAddr(void* addr)
{
  omBinPageRegion region = om_CurrentBinPageRegion;

  if (region == NULL) return 0;
  region = omGListLast(region, prev);
  do
  {
    if ((char *)addr >= region->addr
    && (char *)addr < region->addr + (region->pages)*SIZEOF_SYSTEM_PAGE)
      return region;
    region = region->next;
  }
  while (region != NULL);
  return NULL;
}

int omIsAddrOnFreeBinPage(void* addr)
{
  char *c_addr=(char *)addr;
  omBinPageRegion region = om_CurrentBinPageRegion;

  if (region == NULL) return 0;
  do
  {
    if (c_addr > region->addr && c_addr < region->addr + (region->pages)*SIZEOF_SYSTEM_PAGE)
    {
      if (omIsOnList(region->current, omGetPageOfAddr(addr))) return 1;
      return 0;
    }
    region = region->next;
  }
  while (region != NULL);
  return 0;
}

#endif /* ! OM_NDEBUG */