File: ikarus-winmmap.c

package info (click to toggle)
ikarus 0.0.3%2Bbzr.2010.01.26-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,876 kB
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 266; awk: 186; perl: 66
file content (111 lines) | stat: -rw-r--r-- 2,781 bytes parent folder | download | duplicates (6)
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
/*
 *  Ikarus Scheme -- A compiler for R6RS Scheme.
 *  Copyright (C) 2006,2007,2008  Abdulaziz Ghuloum
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *  
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifdef __CYGWIN__


#include <sys/mman.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>

#define pagesize 4096
#define pageshift 12
#define segment_size (16*pagesize)
#define segment_shift (4+pageshift)
#include "ikarus-winmmap.h"

/* vim:syntax=c */

static unsigned short* table = 0;
static char* ap = 0;
static size_t as = 0;

static void*
do_mmap(size_t n){
  void* x = mmap(0, n, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  if(x == (void*)-1){
    fprintf(stderr, "failed to mmap: %s\n", strerror(errno));
    exit(-1);
  }
  assert((((unsigned int)x) & (segment_size-1)) == 0);
  return x;
}

static void
do_munmap(void* addr, size_t size){
  int err = munmap(addr, size);
  if(err){
   fprintf(stderr, "failed to unmap\n");
   exit(-1);
  }
}


static void
init_table(){
  assert(sizeof(unsigned short) == 2);
  table = do_mmap(32*pagesize);
  bzero(table, 32*pagesize);
}

void
win_munmap(char* addr, size_t size){
  while(size){
    unsigned int page = (((unsigned int) addr) >> pageshift);
    unsigned int segment_index = page / 16;
    unsigned int page_index = page & 15;
    unsigned short alloc_bits = table[segment_index];
    assert((alloc_bits & (1<<page_index)) == (1<<page_index)); 
    unsigned int new_bits = alloc_bits & ~ (1 << page_index);
    table[segment_index] = new_bits;
    if(new_bits == 0){
      do_munmap((void*)(segment_index*segment_size), segment_size);
    }
    size -= pagesize;
    addr += pagesize;
  }
}


char* 
win_mmap(size_t size){
  if(size <= as){
    char* x = ap;
    ap += size;
    as -= size;
    return x;
  }
  if(table == 0) init_table();
  win_munmap(ap, as);
  size_t segments = ((size+segment_size-1) >> segment_shift);
  size_t aligned_size = segments << segment_shift;
  char* addr = do_mmap(aligned_size);
  unsigned int page = (((unsigned int) addr) >> pageshift); 
  unsigned int segment_index = page / 16;
  int i; 
  for(i=0; i<segments; i++){
    table[segment_index+i] = -1;
  }
  ap = addr + size;
  as = aligned_size - size;
  return addr;
} 

#endif