summaryrefslogtreecommitdiffstats
path: root/internals.h
blob: 1f2bdd2e3b934e704357366345204e72e38b37c4 (plain)
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
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * internals.h
 *
 * Internals for LPSM.  Not to be used by applications.
 */

#ifndef LPSM_INTERNALS_H
#define LPSM_INTERNALS_H

#include "lpsm.h"		/* Main include file */
#include "system.h"		/* System-specific constants */

/* Debugging print statements */
#ifdef PRINT_DEBUG_INFO
#include <stdio.h>
#define DPRINTF(X) printf X
#else
#define DPRINTF(X)
#endif

/* Arena definitions */

struct lpsm_arena {
  int main_fd;			/* Primary file descriptor */
  int log_fd;			/* Log file descriptor */
  int pagesize;			/* Page size */
  int pageshift;		/* log2(pagesize) */
  void *arena;			/* Mapped memory zone */
  size_t arena_len;		/* Length of arena */
  char *pageinfo;		/* Page info pointer */
  size_t loaded_count;		/* Loaded pages count (accounting) */
  size_t dirty_count;		/* Dirty pages count (accounting) */
  struct sigaction oldact;	/* Previous signal action */
  size_t fork_seq;		/* Sequence number of forked processes */
};

enum lpsm_recordtype {
  osrec_page,			/* Page data */
  osrec_commit,			/* Commit record */
};

#define LOGRECORD_MAGIC 0x9247746e

struct lpsm_logrecord {
  unsigned int  magic;		/* Magic number; for verification */
  unsigned int record_type;	/* Record */
  size_t size;			/* Data byte count (sequence # for commit) */
  off_t offset;			/* Offset of data */
};

extern struct lpsm_arena *lpsm_memory_info;
#define PM lpsm_memory_info	/* Shorthand */

/* This might end up being a bitmask eventually */
enum page_status {
  page_clean = 0,
  page_dirty = 1,
};

/* Memory management definitions */

#define SLAB_MAGIC		0xec2062ae

struct slab_info {
  int size;			/* Size of SLAB */
  int count;			/* SLABs per page */
  int data_offset;		/* Start of data area */
  int total_pages;		/* Count of pages allocaed to this size SLAB */
  int alloc_slabs;		/* Count of SLABs currently in use */
  struct slab_header *list;
};

/* This is 16 bytes on 32-bit and 24 bytes on 64-bit architectures */
/* This header must end aligned to a bitscan_t datum */
struct slab_header {
  uint32_t magic;
  uint16_t free_count;
  uint16_t index;		/* Index into slab_info array */
  struct slab_header *next, **rev;
};

#define SLAB_INFO_COUNT	32

struct arena_header {
  unsigned char arena_magic[16];
  uint32_t arena_byteorder;
  uint16_t arena_bytesize;
  uint16_t arena_ptrsize;
  void *arena_base;		/* Base of allocation */
  void *free_bitmap;		/* The free bitmap */
  void *alloc_bitmap;		/* The allocated (buddy) bitmap */
  void *data_start;		/* Beginning of data area */
  int buddy_order_min;		/* Minimum size of buddy allocation */
  int arena_size_lg2;		/* Current arena allocation size */
  int header_size;		/* Size of this header+padding */
  struct slab_info slab[SLAB_INFO_COUNT];
  void *root_data_ptrs[LPSM_ROOT_POINTERS]; /* Root data pointers - for the user */
};

extern struct arena_header *lpsm_arena_header;
#define AH lpsm_arena_header	/* Shorthand */

/* This is the minimal order before going to SLAB allocation. */
#define BUDDY_ORDER_MIN		12
#define BUDDY_SIZE_MIN		(1L << BUDDY_ORDER_MIN)

/* Align allocations at least this large (2^n) to this boundary */
#define MINIMUM_ALIGN_LG2	4
#define MINIMUM_ALIGN		(1L << MINIMUM_ALIGN_LG2)
#define MINIMUM_ALIGN_MASK	(MINIMUM_ALIGN-1)

/* This is the size (2^n) allocated for a brand new arena.
   This allocation will be sparse, so don't freak out too much! */
#define ARENA_START_SIZE_LG2	17 /* 128K */
#define ARENA_START_SIZE	(1L << ARENA_START_SIZE_LG2)

/* Function definitions used across modules */

struct buddy_params {
  int xbit;
  int rorder;
};
struct buddy_params lpsm_find_alloc_buddy(const void *ptr);
int lpsm_extend_arena(int logsize);
int lpsm_malloc_buddy_xbit(int order);
void *lpsm_malloc_buddy(size_t size);
void *lpsm_malloc_slab(size_t size);
void lpsm_free_buddy(void *ptr);
void lpsm_free_slab(void *ptr);

#ifdef PRINT_DEBUG_INFO
void lpsm_print_statistics(void);
#else
#define lpsm_print_statistics() ((void)0)
#endif

#endif