mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-25 17:51:01 +00:00
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@7949 72102866-910b-0410-8b05-ffd578937521
190 lines
3.7 KiB
Objective-C
190 lines
3.7 KiB
Objective-C
/* Implementation of page-related functions for GNUstep
|
|
Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
|
Created: May 1996
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
License along with this library; if not, write to the Free
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
|
*/
|
|
|
|
#include <config.h>
|
|
#include <base/preface.h>
|
|
#include <Foundation/NSObjCRuntime.h>
|
|
#include <Foundation/NSZone.h>
|
|
#include <string.h>
|
|
#ifdef __WIN32__
|
|
#include <malloc.h>
|
|
#endif
|
|
#ifndef __MINGW__
|
|
#include <unistd.h>
|
|
#endif
|
|
#include <stdio.h>
|
|
#if __mach__
|
|
#include <mach.h>
|
|
#endif
|
|
|
|
#if __linux__
|
|
#include <linux/kernel.h>
|
|
#include <linux/sys.h>
|
|
#endif
|
|
|
|
#ifdef __MINGW__
|
|
#include <malloc.h>
|
|
static size_t
|
|
getpagesize(void)
|
|
{
|
|
SYSTEM_INFO info;
|
|
GetSystemInfo(&info);
|
|
return info.dwPageSize;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __SOLARIS__
|
|
#define getpagesize() sysconf(_SC_PAGESIZE)
|
|
#endif
|
|
|
|
#ifdef __svr4__
|
|
#define getpagesize() sysconf(_SC_PAGESIZE)
|
|
#endif
|
|
|
|
#if __mach__
|
|
#define getpagesize vm_page_size
|
|
#endif
|
|
|
|
/* 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;
|
|
|
|
/* Return the number of bytes in a memory page. */
|
|
unsigned
|
|
NSPageSize (void)
|
|
{
|
|
if (!ns_page_size)
|
|
ns_page_size = (unsigned) getpagesize ();
|
|
return ns_page_size;
|
|
}
|
|
|
|
/* Return log base 2 of the number of bytes in a memory page. */
|
|
unsigned
|
|
NSLogPageSize (void)
|
|
{
|
|
unsigned tmp_page_size = NSPageSize();
|
|
unsigned log = 0;
|
|
|
|
while (tmp_page_size >>= 1)
|
|
log++;
|
|
return log;
|
|
}
|
|
|
|
/* Round BYTES down to the nearest multiple of the memory page size,
|
|
and return it. */
|
|
unsigned
|
|
NSRoundDownToMultipleOfPageSize (unsigned bytes)
|
|
{
|
|
unsigned a = NSPageSize();
|
|
|
|
return (bytes / a) * a;
|
|
}
|
|
|
|
/* Round BYTES up to the nearest multiple of the memory page size,
|
|
and return it. */
|
|
unsigned
|
|
NSRoundUpToMultipleOfPageSize (unsigned bytes)
|
|
{
|
|
unsigned a = NSPageSize();
|
|
|
|
return ((bytes % a) ? ((bytes / a + 1) * a) : bytes);
|
|
}
|
|
|
|
#if __linux__
|
|
#include <sys/sysinfo.h>
|
|
#endif
|
|
|
|
unsigned
|
|
NSRealMemoryAvailable ()
|
|
{
|
|
#if __linux__
|
|
struct sysinfo info;
|
|
|
|
if ((sysinfo(&info)) != 0)
|
|
return 0;
|
|
return (unsigned) info.freeram;
|
|
#else
|
|
fprintf (stderr, "NSRealMemoryAvailable() not implemented.\n");
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
void *
|
|
NSAllocateMemoryPages (unsigned bytes)
|
|
{
|
|
void *where;
|
|
#if __mach__
|
|
kern_return_t r;
|
|
r = vm_allocate (mach_task_self(), &where, (vm_size_t) bytes, 1);
|
|
if (r != KERN_SUCCESS)
|
|
return NULL;
|
|
return where;
|
|
#else
|
|
#if HAVE_VALLOC
|
|
where = valloc (bytes);
|
|
#else
|
|
where = malloc (bytes);
|
|
#endif
|
|
if (where == NULL)
|
|
return NULL;
|
|
memset (where, 0, bytes);
|
|
return where;
|
|
#endif
|
|
}
|
|
|
|
void
|
|
NSDeallocateMemoryPages (void *ptr, unsigned bytes)
|
|
{
|
|
#if __mach__
|
|
vm_deallocate (mach_task_self (), ptr, bytes);
|
|
#else
|
|
free (ptr);
|
|
#endif
|
|
}
|
|
|
|
void
|
|
NSCopyMemoryPages (const void *source, void *dest, unsigned bytes)
|
|
{
|
|
#if __mach__
|
|
kern_return_t r;
|
|
r = vm_copy (mach_task_self(), source, bytes, dest);
|
|
NSParameterAssert (r == KERN_SUCCESS);
|
|
#else
|
|
memcpy (dest, source, bytes);
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|