2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation of page-related functions for GNUstep
|
1997-01-06 22:34:37 +00:00
|
|
|
Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
Created: May 1996
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2005-05-22 03:32:16 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSPage class reference</title>
|
|
|
|
$Date$ $Revision$
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "config.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
#include "GNUstepBase/preface.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSObjCRuntime.h"
|
|
|
|
#include "Foundation/NSZone.h"
|
1997-12-11 19:09:56 +00:00
|
|
|
#include <string.h>
|
2000-06-13 14:50:40 +00:00
|
|
|
#ifdef __WIN32__
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2002-05-02 21:22:06 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1996-05-28 20:53:06 +00:00
|
|
|
#include <unistd.h>
|
1996-07-15 18:41:44 +00:00
|
|
|
#endif
|
1996-05-28 20:53:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#if __mach__
|
|
|
|
#include <mach.h>
|
|
|
|
#endif
|
|
|
|
|
1998-01-26 14:18:18 +00:00
|
|
|
#if __linux__
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sys.h>
|
|
|
|
#endif
|
|
|
|
|
2000-06-12 05:17:41 +00:00
|
|
|
#ifdef __MINGW__
|
2000-06-06 16:50:52 +00:00
|
|
|
#include <malloc.h>
|
2005-02-22 11:22:44 +00:00
|
|
|
static size_t
|
2000-06-06 16:50:52 +00:00
|
|
|
getpagesize(void)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
GetSystemInfo(&info);
|
|
|
|
return info.dwPageSize;
|
|
|
|
}
|
1996-07-15 18:41:44 +00:00
|
|
|
#endif
|
|
|
|
|
1996-10-31 18:34:51 +00:00
|
|
|
#ifdef __SOLARIS__
|
|
|
|
#define getpagesize() sysconf(_SC_PAGESIZE)
|
|
|
|
#endif
|
1998-05-29 15:25:41 +00:00
|
|
|
|
|
|
|
#ifdef __svr4__
|
|
|
|
#define getpagesize() sysconf(_SC_PAGESIZE)
|
|
|
|
#endif
|
1996-10-31 18:34:51 +00:00
|
|
|
|
|
|
|
#if __mach__
|
|
|
|
#define getpagesize vm_page_size
|
|
|
|
#endif
|
|
|
|
|
2003-01-07 18:33:51 +00:00
|
|
|
#ifdef __BEOS__
|
2002-12-29 01:50:31 +00:00
|
|
|
#include <kernel/OS.h>
|
|
|
|
#define getpagesize() B_PAGE_SIZE
|
|
|
|
#endif
|
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
/* Cache the size of a memory page here, so we don't have to make the
|
|
|
|
getpagesize() system call repeatedly. */
|
|
|
|
static unsigned ns_page_size = 0;
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Return the number of bytes in a memory page.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
unsigned
|
|
|
|
NSPageSize (void)
|
|
|
|
{
|
|
|
|
if (!ns_page_size)
|
|
|
|
ns_page_size = (unsigned) getpagesize ();
|
|
|
|
return ns_page_size;
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Return log base 2 of the number of bytes in a memory page.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
unsigned
|
|
|
|
NSLogPageSize (void)
|
|
|
|
{
|
1997-01-09 19:13:50 +00:00
|
|
|
unsigned tmp_page_size = NSPageSize();
|
1997-01-06 22:34:37 +00:00
|
|
|
unsigned log = 0;
|
1996-05-28 20:53:06 +00:00
|
|
|
|
1997-01-06 22:34:37 +00:00
|
|
|
while (tmp_page_size >>= 1)
|
1996-05-28 20:53:06 +00:00
|
|
|
log++;
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Round bytes down to the nearest multiple of the memory page size,
|
|
|
|
* and return it.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
unsigned
|
|
|
|
NSRoundDownToMultipleOfPageSize (unsigned bytes)
|
|
|
|
{
|
1997-01-09 19:13:50 +00:00
|
|
|
unsigned a = NSPageSize();
|
1997-01-06 21:35:19 +00:00
|
|
|
|
|
|
|
return (bytes / a) * a;
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Round bytes up to the nearest multiple of the memory page size,
|
|
|
|
* and return it.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
unsigned
|
|
|
|
NSRoundUpToMultipleOfPageSize (unsigned bytes)
|
|
|
|
{
|
1997-01-09 19:13:50 +00:00
|
|
|
unsigned a = NSPageSize();
|
1997-01-06 21:35:19 +00:00
|
|
|
|
|
|
|
return ((bytes % a) ? ((bytes / a + 1) * a) : bytes);
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
#if __linux__
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Return the number of bytes of real (physical) memory available.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
unsigned
|
|
|
|
NSRealMemoryAvailable ()
|
|
|
|
{
|
1998-01-26 14:18:18 +00:00
|
|
|
#if __linux__
|
|
|
|
struct sysinfo info;
|
|
|
|
|
|
|
|
if ((sysinfo(&info)) != 0)
|
|
|
|
return 0;
|
|
|
|
return (unsigned) info.freeram;
|
2003-01-07 18:33:51 +00:00
|
|
|
#elif defined(__MINGW__)
|
2002-12-31 17:13:30 +00:00
|
|
|
MEMORYSTATUS memory;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-12-31 17:13:30 +00:00
|
|
|
GlobalMemoryStatus(&memory);
|
|
|
|
return (unsigned)memory.dwAvailPhys;
|
2003-01-07 18:33:51 +00:00
|
|
|
#elif defined(__BEOS__)
|
2002-12-31 17:13:30 +00:00
|
|
|
system_info info;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-12-31 17:13:30 +00:00
|
|
|
if (get_system_info(&info) != B_OK)
|
|
|
|
return 0;
|
|
|
|
return (unsigned)(info.max_pages - info.used_pages) * B_PAGE_SIZE;
|
1998-01-26 14:18:18 +00:00
|
|
|
#else
|
1996-05-28 20:53:06 +00:00
|
|
|
fprintf (stderr, "NSRealMemoryAvailable() not implemented.\n");
|
|
|
|
return 0;
|
1998-01-26 14:18:18 +00:00
|
|
|
#endif
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
2004-07-29 15:30:47 +00:00
|
|
|
* Allocate memory for this process and return a pointer to it (or a null
|
2005-02-22 11:22:44 +00:00
|
|
|
* pointer on failure).
|
2002-12-31 19:31:46 +00:00
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
void *
|
|
|
|
NSAllocateMemoryPages (unsigned bytes)
|
|
|
|
{
|
|
|
|
void *where;
|
1997-01-06 21:35:19 +00:00
|
|
|
#if __mach__
|
1996-05-28 20:53:06 +00:00
|
|
|
kern_return_t r;
|
|
|
|
r = vm_allocate (mach_task_self(), &where, (vm_size_t) bytes, 1);
|
1997-03-03 19:46:52 +00:00
|
|
|
if (r != KERN_SUCCESS)
|
|
|
|
return NULL;
|
1996-05-28 20:53:06 +00:00
|
|
|
return where;
|
|
|
|
#else
|
2002-05-02 21:22:06 +00:00
|
|
|
#ifdef HAVE_VALLOC
|
1997-01-06 21:35:19 +00:00
|
|
|
where = valloc (bytes);
|
2000-06-13 14:50:40 +00:00
|
|
|
#else
|
|
|
|
where = malloc (bytes);
|
|
|
|
#endif
|
1997-03-03 19:46:52 +00:00
|
|
|
if (where == NULL)
|
|
|
|
return NULL;
|
1997-01-06 21:35:19 +00:00
|
|
|
memset (where, 0, bytes);
|
|
|
|
return where;
|
1996-05-28 20:53:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Deallocate memory which was previously allocated using the
|
|
|
|
* NSAllocateMemoryPages() function.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
void
|
|
|
|
NSDeallocateMemoryPages (void *ptr, unsigned bytes)
|
|
|
|
{
|
|
|
|
#if __mach__
|
|
|
|
vm_deallocate (mach_task_self (), ptr, bytes);
|
|
|
|
#else
|
|
|
|
free (ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Perform an efficient large scale copy of data from src to dest.
|
|
|
|
* The value bytes specifies the length of the data copied.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
void
|
2002-12-31 19:31:46 +00:00
|
|
|
NSCopyMemoryPages (const void *src, void *dest, unsigned bytes)
|
1996-05-28 20:53:06 +00:00
|
|
|
{
|
|
|
|
#if __mach__
|
|
|
|
kern_return_t r;
|
2002-12-31 19:31:46 +00:00
|
|
|
r = vm_copy (mach_task_self(), src, bytes, dest);
|
1996-05-28 20:53:06 +00:00
|
|
|
NSParameterAssert (r == KERN_SUCCESS);
|
|
|
|
#else
|
2002-12-31 19:31:46 +00:00
|
|
|
memcpy (dest, src, bytes);
|
1996-05-28 20:53:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|