1/* BEGIN LICENSE BLOCK 2 * Version: CMPL 1.1 3 * 4 * The contents of this file are subject to the Cisco-style Mozilla Public 5 * License Version 1.1 (the "License"); you may not use this file except 6 * in compliance with the License. You may obtain a copy of the License 7 * at www.eclipse-clp.org/license. 8 * 9 * Software distributed under the License is distributed on an "AS IS" 10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11 * the License for the specific language governing rights and limitations 12 * under the License. 13 * 14 * The Original Code is The ECLiPSe Constraint Logic Programming System. 15 * The Initial Developer of the Original Code is Cisco Systems, Inc. 16 * Portions created by the Initial Developer are 17 * Copyright (C) 1992-2006 Cisco Systems, Inc. All Rights Reserved. 18 * 19 * Contributor(s): Joachim Schimpf, ECRC. 20 * 21 * END LICENSE BLOCK */ 22/*--------------------------------------------------------------------- 23 * IDENTIFICATION memory.h 24 * 25 * AUTHOR Joachim Schimpf 26 * 27 * DESCRIPTION see alloc.c shared_mem.c private_mem.c lock.s 28 * 29 * USAGE: include this file, link with libshm.a 30 *---------------------------------------------------------------------*/ 31 32#ifndef __ECLIPSE_MEMMAN_H 33#define __ECLIPSE_MEMMAN_H 34 35#ifdef HAVE_NO_VOID_PTR 36typedef char *generic_ptr; 37#else 38typedef void *generic_ptr; 39#endif 40 41/*--------------------------------------------------------------------- 42 * Size-dependent values 43 *---------------------------------------------------------------------*/ 44 45// As per C standard: In any particular implementation, a 46// plain char object can take on either the same values as 47// a signed char or an unsigned char; which one is 48// implementation-defined. 49typedef signed char int8; /* exactly 8 bit */ 50typedef unsigned char uint8; 51 52typedef short int16; /* exactly 16 bit */ 53typedef unsigned short uint16; 54 55#if (SIZEOF_INT == 4) 56typedef int int32; /* exactly 32 bit */ 57typedef unsigned int uint32; 58#endif 59 60#if (SIZEOF_CHAR_P == SIZEOF_INT) 61typedef int word; /* pointer-sized */ 62typedef unsigned int uword; 63#define WSUF(X) X 64#elif (SIZEOF_CHAR_P == SIZEOF_LONG) 65typedef long word; /* pointer-sized */ 66typedef unsigned long uword; 67#define WSUF(X) (X##L) 68#elif (defined(HAVE_LONG_LONG) || defined(__GNUC__)) && \ 69 (SIZEOF_CHAR_P == __SIZEOF_LONG_LONG__) 70typedef long long word; /* pointer-sized */ 71typedef unsigned long long uword; 72#define WSUF(X) (X##LL) 73#elif (defined(HAVE___INT64) && SIZEOF_CHAR_P == 8) 74typedef __int64 word; 75typedef unsigned __int64 uword; 76#define WSUF(X) (X##I64) 77#endif /* no code for dealing with word size > long long/__int64! */ 78 79 80#if (SIZEOF_CHAR_P == 8) 81/* Maximal representable address divided by bits per byte. */ 82/* For -taso we address only the 32-bit memory */ 83#define MAX_ADDRESS_BYTE 0x20000000 84#ifndef SIGN_BIT 85#define SIGN_BIT ((uword) WSUF(0x8000000000000000)) 86#endif 87#else 88#define MAX_ADDRESS_BYTE 0x20000000 89#ifndef SIGN_BIT 90#define SIGN_BIT ((uword) 0x80000000L) 91#endif 92#endif 93 94 95/* the unit of allocation */ 96 97typedef union 98{ 99 struct 100 { 101 word a1, a2; /* seems resonable not to allocate smaller */ 102 } p; 103 double d; /* may be same or less than 2 pointer sizes */ 104} unit_type; 105 106/*--------------------------------------------------------------------- 107 * Logical page manager 108 *---------------------------------------------------------------------*/ 109 110#define BYTES_PER_UNIT sizeof(unit_type) 111#define BYTES_PER_PAGE 4096 /* logical page size =< physical */ 112#define UNITS_PER_PAGE (BYTES_PER_PAGE/BYTES_PER_UNIT) 113#define WORDS_PER_PAGE (BYTES_PER_PAGE/sizeof(bits32)) 114#define BITMAP_BLOCKSIZE BYTES_PER_PAGE 115#define BITMAP_BLOCKS (MAX_ADDRESS_BYTE/BYTES_PER_PAGE/BITMAP_BLOCKSIZE) 116#define PAGE_LISTS 32 117#define MIN_OS_PAGE_REQUEST 8 /* min pages to get from OS */ 118 119typedef uint32 bits32; 120 121struct cluster { 122 struct cluster *next; 123 generic_ptr addr; 124 word size; 125 word dummy; 126}; 127 128struct page_log { 129 generic_ptr addr; 130 word npages; 131}; 132 133struct page_admin { 134 word allocated; /* # pages gotten from OS */ 135 word freed; /* # pages in free list */ 136 generic_ptr min_addr, max_addr; 137 struct page_log *log_page; /* log of more'd pages */ 138 word log_idx; 139 struct cluster *free[PAGE_LISTS]; /* free[i]: i-page-clusters */ 140 /* free[0]: larger clusters */ 141 bits32 *map[BITMAP_BLOCKS]; /* bitmap of pages (1 = free) */ 142}; 143 144 145/*--------------------------------------------------------------------- 146 * Block manager 147 *---------------------------------------------------------------------*/ 148 149#define HEAP_STAT_ALLOCATED 0 150#define HEAP_STAT_USED 1 151 152#define LARGEST_SMALL_BLOCK 7 /* units */ 153#define SMALLEST_POWER_BLOCK 8 /* units */ 154#define SMALLEST_PAGE_BLOCK (BYTES_PER_PAGE/BYTES_PER_UNIT) /* units */ 155#define LARGEST_POWER_BLOCK (SMALLEST_PAGE_BLOCK/2) 156#define POWER_FIRST_INDEX 6 157#define POWERS 32 158 159struct heap { 160 generic_ptr small_blocks[LARGEST_SMALL_BLOCK+1]; 161 generic_ptr powers[POWERS]; 162 163 generic_ptr alloc_ptr; 164 word alloc_free; /* in heap_units */ 165 166 /* statistics */ 167 168 word small_allocated[LARGEST_SMALL_BLOCK+1]; 169 word powers_allocated[POWERS]; 170 word requested, /* in bytes */ 171 used, /* small/power only (in heap_units) */ 172 allocs, 173 small_block_pages, 174 power_pages; 175}; 176 177 178/*--------------------------------------------------------------------- 179 * Allocation with headers 180 *---------------------------------------------------------------------*/ 181 182typedef union mem_header 183{ 184 struct 185 { 186 struct heap *magic; 187 word size; 188 } s; 189 double dummy; /* force alignment of blocks */ 190} HEADER; 191 192 193/*--------------------------------------------------------------------- 194 * Interrupt disabling 195 *---------------------------------------------------------------------*/ 196 197#define InterruptsDisabled it_disabled_ 198#define Disable_Int() it_disabled_++; 199#define Enable_Int() \ 200 { if (--it_disabled_ == 0 && delayed_it_) (*delayed_irq_func)(); } 201#define InterruptsPending delayed_it_ 202#define Set_Interrupts_Pending() delayed_it_ = 1; 203#define Clr_Interrupts_Pending() delayed_it_ = 0; 204 205extern volatile int it_disabled_, delayed_it_; 206#ifdef __STDC__ 207extern void (*delayed_irq_func)(void); 208#else 209extern void (*delayed_irq_func)(); 210#endif 211 212/*--------------------------------------------------------------------- 213 * Spin Locks 214 *---------------------------------------------------------------------*/ 215 216#if (defined(_PA_RISC1_0) || defined(_PA_RISC1_1)) 217typedef int a_mutex_t[4]; 218#else 219typedef int a_mutex_t; 220#endif 221 222/*--------------------------------------------------------------------- 223 * Heap descriptor, lowest level 224 *---------------------------------------------------------------------*/ 225 226/* The private memory part */ 227 228struct heap_descriptor { 229 struct shm_desc *shared_header; /* NULL for private memory */ 230 struct page_admin *pages; 231 struct heap *heap; 232 int map_fd; 233#ifdef __STDC__ 234 generic_ptr (*more)(word,int,struct heap_descriptor*); 235 int (*less)(generic_ptr,word,struct heap_descriptor*); 236 void (*panic)(const char*, const char*); 237#else 238 generic_ptr (*more)(); 239 generic_ptr (*less)(); 240 void (*panic)(); 241#endif 242 int debug_level; 243}; 244 245/* The shared memory part (only if really shared) */ 246 247struct shm_desc { 248 generic_ptr application_header; /* must be the first word! */ 249 char *start; /* own address */ 250 char *brk; /* end of allocated space */ 251 char *lim; /* end of the mapped region */ 252 char *stop; /* end of the reserved address space */ 253 int incr; /* mapping increment in bytes */ 254 int processes; /* number of attached processes */ 255 char *mapfile; /* file it is mapped to */ 256 a_mutex_t lock; /* memory management lock */ 257 struct heap heap; /* block manager structure */ 258 struct page_admin pages; /* page manager structure */ 259 char mapfile_buf[1024]; /* string buffer for mapfile name */ 260}; 261 262 263/*--------------------------------------------------------------------- 264 * Simplified private heap interface 265 *---------------------------------------------------------------------*/ 266 267extern struct heap_descriptor private_heap; 268 269 270/*--------------------------------------------------------------------- 271 * Function prototypes 272 *---------------------------------------------------------------------*/ 273 274#ifdef __STDC__ 275 276void pagemanager_init(struct heap_descriptor *); 277void pagemanager_fini(struct heap_descriptor *); 278generic_ptr alloc_page(struct heap_descriptor *); 279generic_ptr alloc_pagewise(struct heap_descriptor *, word, word *); 280void free_pages(struct heap_descriptor *, generic_ptr, word); 281 282void irq_lock_init(void (*irq_fct)(void)); 283int a_mutex_init(a_mutex_t *); 284int a_mutex_lock(volatile a_mutex_t *); 285int a_mutex_unlock(a_mutex_t *); 286int a_mutex_destroy(a_mutex_t *); 287 288void alloc_init(struct heap_descriptor *); 289void alloc_debug_level(struct heap_descriptor *, int); 290generic_ptr alloc_size(struct heap_descriptor *, word); 291void free_size(struct heap_descriptor *, generic_ptr, word); 292generic_ptr realloc_size(struct heap_descriptor *, generic_ptr, word, word); 293generic_ptr h_alloc(struct heap_descriptor *, word); 294void h_free(struct heap_descriptor *, generic_ptr); 295generic_ptr h_realloc(struct heap_descriptor *, generic_ptr, word); 296int address_in_heap(struct heap_descriptor *, generic_ptr); 297int alloc_statistics(struct heap_descriptor *, int); 298 299generic_ptr hp_alloc_size(word size); 300void hp_free_size(generic_ptr, word size); 301generic_ptr hp_realloc_size(generic_ptr, word, word); 302generic_ptr hp_alloc(word size); 303void hp_free(generic_ptr); 304generic_ptr hp_resize(generic_ptr, word); 305int hp_statistics(int what); 306 307char *shared_mem_base(void); 308char *shared_mem_init(int create_flag, 309 char* mapfile, char* start, 310 word size, word increment, 311 void (*panic_fct)(const char*, const char*), 312 struct heap_descriptor *hd); 313void shared_mem_release(struct heap_descriptor *hd); 314int shared_mem_save(struct heap_descriptor *hd, int fd); 315int shared_mem_restore(struct heap_descriptor *hd, int fd); 316void private_mem_init(void (*panic_fct)(const char*, const char*)); 317char * private_mem_init_desc(void (*panic_fct)(const char*, const char*), 318 struct heap_descriptor *hd); 319void private_mem_fini(); 320void private_mem_fini_desc(struct heap_descriptor *hd); 321 322 323#else /* __STDC__ */ 324 325void pagemanager_init(); 326generic_ptr alloc_page(); 327generic_ptr alloc_pagewise(); 328void free_pages(); 329 330void irq_lock_init(); 331int a_mutex_init(); 332int a_mutex_lock(); 333int a_mutex_unlock(); 334int a_mutex_destroy(); 335 336void alloc_init(); 337generic_ptr alloc_size(); 338void free_size(); 339generic_ptr realloc_size(); 340generic_ptr h_alloc(); 341void h_free(); 342generic_ptr h_realloc(); 343int address_in_heap(); 344int alloc_statistics(); 345 346generic_ptr hp_alloc_size(); 347void hp_free_size(); 348generic_ptr hp_realloc_size(); 349generic_ptr hp_alloc(); 350void hp_free(); 351generic_ptr hp_resize(); 352int hp_statistics(); 353 354char *shared_mem_base(); 355char *shared_mem_init(); 356void shared_mem_release(); 357int shared_mem_save(); 358int shared_mem_restore(); 359void private_mem_init(); 360char * private_mem_init_desc(); 361 362 363#endif /* __STDC__ */ 364 365#endif /* __ECLIPSE_MEMMAN_H */ 366