OSMem.h 2.99 KB
Newer Older
1 2
/* -----------------------------------------------------------------------------
 *
3
 * (c) The University of Glasgow 2006-2008
4 5 6 7 8
 *
 * OS-specific memory management
 *
 * ---------------------------------------------------------------------------*/

Simon Marlow's avatar
Simon Marlow committed
9 10 11
#ifndef SM_OSMEM_H
#define SM_OSMEM_H

12
#include "BeginPrivate.h"
13

14
void osMemInit(void);
15
void *osGetMBlocks(uint32_t n);
16
void osFreeMBlocks(void *addr, uint32_t n);
17
void osReleaseFreeMemory(void);
18
void osFreeAllMBlocks(void);
19
size_t getPageSize (void);
Ben Gamari's avatar
Ben Gamari committed
20
StgWord64 getPhysicalMemorySize (void);
Ben Gamari's avatar
Ben Gamari committed
21 22
void setExecutable (void *p, W_ len, bool exec);
bool osNumaAvailable(void);
Simon Marlow's avatar
Simon Marlow committed
23
uint32_t osNumaNodes(void);
24
uint64_t osNumaMask(void);
Simon Marlow's avatar
Simon Marlow committed
25
void osBindMBlocksToNode(void *addr, StgWord size, uint32_t node);
Simon Marlow's avatar
Simon Marlow committed
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
INLINE_HEADER size_t
roundDownToPage (size_t x)
{
    size_t size = getPageSize();
    return (x & ~(size - 1));
}

INLINE_HEADER size_t
roundUpToPage (size_t x)
{
    size_t size = getPageSize();
    return ((x + size - 1) & ~(size - 1));
}


42 43 44 45 46 47 48 49 50
#ifdef USE_LARGE_ADDRESS_SPACE

/*
  If "large address space" is enabled, we allocate memory in two
  steps: first we request some address space, and then we request some
  memory in it. This allows us to ask for much more address space that
  we will ever need, which keeps everything nice and consecutive.
*/

51 52 53 54
// Reserve the large address space blob of the given size, and return the
// address that the OS has chosen for it. It is not safe to access the memory
// pointed to by the return value, until that memory is committed using
// osCommitMemory().
55
//
56 57 58 59
// The value pointed to by len will be filled by the caller with an upper
// bound on the amount of memory to reserve. On return this will be set
// to the amount of memory actually reserved.
//
60
// This function is called once when the block allocator is initialized.
61 62 63 64
//
// startAddress must be greater or equal than 8 * (1 << 30), and can be
// NULL, in which case a default will be picked by the RTS.
void *osReserveHeapMemory(void *startAddress, W_ *len);
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

// Commit (allocate memory for) a piece of address space, which must
// be within the previously reserved space After this call, it is safe
// to access @p up to @len bytes.
//
// There is no guarantee on the contents of the memory pointed to by
// @p, in particular it must not be assumed to contain all zeros.
void osCommitMemory(void *p, W_ len);

// Decommit (release backing memory for) a piece of address space,
// which must be within the previously reserve space and must have
// been previously committed After this call, it is again unsafe to
// access @p (up to @len bytes), but there is no guarantee that the
// memory will be released to the system (as far as eg. RSS statistics
// from top are concerned).
void osDecommitMemory(void *p, W_ len);

// Release the address space previously obtained and undo the effects of
// osReserveHeapMemory
//
// This function is called once, when the block allocator is deinitialized
// before the program terminates.
void osReleaseHeapMemory(void);
#endif

90
#include "EndPrivate.h"
91

Simon Marlow's avatar
Simon Marlow committed
92
#endif /* SM_OSMEM_H */