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/*---------------------------------------------------------------------
24 * IDENTIFICATION	shared_mem_base.c
25 *			(ar truncates the name, therefore shmem_base.c)
26 *
27 * VERSION		$Id: shmem_base.c,v 1.1 2006/09/23 01:56:26 snovello Exp $
28 *
29 * CONTENTS		Computation of the shm base address
30 *---------------------------------------------------------------------*/
31
32#include "config.h"
33
34#ifndef _WIN32
35
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>	/* need the sbrk() declaration */
38#endif
39
40#ifdef HAVE_STRING_H
41#include <string.h>
42#endif
43
44#ifdef STDC_HEADERS
45#include <stdlib.h>
46#else
47extern long strtol();
48extern char *getenv();
49#endif
50
51#define MB			1048576
52#define KB			1024
53
54
55/*
56 * Return a start address that can be used to map a lot of shared memory.
57 * This is very machine-dependent.
58 * The value is chosen taking into account the needs of eclipse:
59 * There must be at least space for a private heap and two mapped
60 * stacks between the brk and the start of shared memory.
61 * A return value of NULL indicates that the choice should be left
62 * to the operating system.
63 * This function is in a separate file so that it can be replaced
64 * if the underlying architecture has a different memory layout.
65 */
66
67char *
68shared_mem_base(void)
69{
70    char *base, *env_shmbase;
71
72    env_shmbase = getenv("ECLIPSE_SHMBASE");
73    if (env_shmbase)
74    {
75	base = (char *) strtol(env_shmbase, 0, 0);
76    }
77    else
78    {
79#ifdef _AIX
80    /* There is an unmappable area between the program and the
81       large mappable pool, so we use fixed addresses. */
82	base = (char *) 0x40000000;
83#else
84#ifdef mips
85    /* sgi_irix52 (mips) starts the program at 0x10000000 */
86	base = (char *) 0x30000000;
87#else
88#if (defined(_PA_RISC1_0) || defined(_PA_RISC1_1))
89	/* HP 9000/700 can only MAP_SHARED between 80000000 and efffffff.
90	 * A range that is MAP_SHARED in one process is reserved by the OS
91	 * for all processes (in case they want to map it there as well).
92	 * Therefore MAP_FIXED does almost never work, and it is best
93	 * to let the OS choose the address.
94	 */
95	base = (char *) 0;
96#else
97#if (defined(sun) && defined(mc68000))
98    /* max virtual address on sun 3 is somewhat above 200 MB */
99	base = (char *) (161*MB);
100#else
101#if (defined(linux) && defined(__ELF__))
102#ifdef __alpha
103        base = (char *) 0x200000000;
104#else
105        base = (char *) 0x20000000;
106#endif
107#else
108#ifdef __alpha
109	base = (char *) 0x55000000;
110#else
111    /* max virtual address on sparc is around 512 MB */
112	base = (char *) (320*MB);
113#endif
114#endif
115#endif
116#endif
117#endif
118#endif
119    }
120
121    /* make sure the address is somewhat above the brk */
122    if (base && (char *) sbrk(0) + 16*MB >= base)
123    {
124	(void) write(2, "ECLiPSe: bad memory layout\n", 27);
125    }
126    return base;
127}
128
129#endif /* _WIN32 */
130