File: omMmap.c

package info (click to toggle)
singular 1%3A4.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,520 kB
  • sloc: cpp: 319,164; ansic: 42,206; perl: 5,855; sh: 5,524; lisp: 4,241; python: 2,101; makefile: 1,890; yacc: 1,651; pascal: 1,411; lex: 1,367; tcl: 1,024; xml: 182
file content (46 lines) | stat: -rw-r--r-- 1,147 bytes parent folder | download | duplicates (5)
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
/*******************************************************************
 *  File:    omMmap.c
 *  Purpose: implementing valloc via mmap
 *  Author:  obachman (Olaf Bachmann)
 *  Created: 11/99
 *******************************************************************/
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#define MAP_ANONYMOUS MAP_ANON
#endif

static inline void* omVallocMmap(size_t size)
{
  void* addr;
#ifndef MAP_ANONYMOUS
  static int fd = -1;
#endif

#ifdef MAP_ANONYMOUS
#ifndef __CYGWIN__
  /* under cygwin, MAP_PRIVATE|MAP_ANONYMOUS fails */
  addr = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
#else
  /* however, the following works */
  addr = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, -1, 0);
#endif
#else /* !MAP_ANONYMOUS */
  if (fd < 0)
  {
    fd = open("/dev/zero", O_RDWR);
    if (fd < 0) return NULL;
  }
  addr = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
#endif

  if (addr == (void*) -1) return NULL;
  return addr;
}

static inline int omVfreeMmap(void* addr, size_t size)
{
  return munmap(addr, size);
}