fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / config / gen / platform / openbsd / memexec.c
blob29e1aacec00195aa65df71b69371c8f50e4908df
1 /*
2 * $Id$
3 * Copyright (C) 2004-2010, Parrot Foundation.
4 */
6 /*
8 =head1 NAME
10 memexec.c
12 =head1 DESCRIPTION
14 Memory protection functions.
16 =head2 Functions
18 =over 4
20 =cut
24 #ifdef PARROT_HAS_EXEC_PROTECT
27 =item C<void * mem_alloc_executable(size_t size)>
29 Allocate executable memory
30 Round up to page size because the whole page will be marked as executable
31 malloc() under OpenBSD page-aligns allocations >= page size
33 =cut
37 void *
38 mem_alloc_executable(size_t size)
40 void *p;
41 size_t pagesize = sysconf(_SC_PAGESIZE);
42 size = (size + pagesize - 1) & ~(pagesize-1);
43 p = malloc(size);
44 if (p != NULL) {
45 mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC);
47 return p;
52 =item C<void mem_free_executable(void *p, size_t size)>
54 Free a buffer allocated with mem_alloc_executable().
56 =cut
60 void
61 mem_free_executable(void *p, size_t size)
63 free(p);
68 =item C<void * mem_realloc_executable(void* oldp, size_t oldsize, size_t
69 newsize)>
71 Reallocate executable memory
72 Round up to page size because the whole page will be marked as executable
74 =cut
78 void *
79 mem_realloc_executable(void* oldp, size_t oldsize, size_t newsize)
81 size_t pagesize = sysconf(_SC_PAGESIZE);
82 size_t roundup = (newsize + pagesize - 1) & ~(pagesize-1);
83 void *newp = realloc(oldp, roundup);
84 if (newp != NULL) {
85 mprotect(newp, roundup, PROT_READ|PROT_WRITE|PROT_EXEC);
87 return newp;
89 #endif
93 =back
95 =cut
100 * Local variables:
101 * c-file-style: "parrot"
102 * End:
103 * vim: expandtab shiftwidth=4: