Boston, MA 02111-1307, USA. */
#include <sys/types.h>
-#include <fcntl.h> /* After sys/types.h, at least for dpx/2. */
+#include <fcntl.h> /* After sys/types.h, at least for dpx/2. */
#include <sys/stat.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
-#include <unistd.h> /* Prototypes for lseek */
+#include <unistd.h> /* Prototypes for lseek */
#endif
#include "mmprivate.h"
#endif
-#if defined(HAVE_MMAP)
-
/* Forward declarations/prototypes for local functions */
-static struct mdesc *reuse PARAMS ((int));
+static struct mdesc *reuse(int fd);
/* Initialize access to a mmalloc managed region.
On failure returns NULL. */
-PTR
-mmalloc_attach (fd, baseaddr)
- int fd;
- PTR baseaddr;
+void *mmalloc_attach(int fd, void *baseaddr)
{
struct mdesc mtemp;
struct mdesc *mdp;
- PTR mbase;
+ void *mbase;
struct stat sbuf;
/* First check to see if FD is a valid file descriptor, and if so, see
obsolete version, or any other reason, then we fail to attach to
this file. */
- if (fd >= 0)
- {
- if (fstat (fd, &sbuf) < 0)
- return (NULL);
+ if (fd >= 0) {
+ if (fstat(fd, &sbuf) < 0)
+ return (NULL);
else if (sbuf.st_size > 0)
- return ((PTR) reuse (fd));
+ return ((void *) reuse(fd));
}
/* If the user provided NULL BASEADDR then fail */
then initialize the fields that we know values for. */
mdp = &mtemp;
- memset ((char *) mdp, 0, sizeof (mtemp));
- strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
- mdp -> headersize = sizeof (mtemp);
- mdp -> version = MMALLOC_VERSION;
- mdp -> morecore = __mmalloc_mmap_morecore;
- mdp -> fd = fd;
- mdp -> base = mdp -> breakval = mdp -> top = baseaddr;
+ memset((char *) mdp, 0, sizeof(mtemp));
+ strncpy(mdp->magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
+ mdp->headersize = sizeof(mtemp);
+ mdp->version = MMALLOC_VERSION;
+ mdp->morecore = __mmalloc_mmap_morecore;
+ mdp->fd = fd;
+ mdp->base = mdp->breakval = mdp->top = baseaddr;
/* If we have not been passed a valid open file descriptor for the file
to map to, then we go for an anonymous map */
- if(mdp -> fd < 0)
- mdp -> flags |= MMALLOC_ANONYMOUS;
+ if (mdp->fd < 0)
+ mdp->flags |= MMALLOC_ANONYMOUS;
/* If we have not been passed a valid open file descriptor for the file
to map to, then open /dev/zero and use that to map to. */
-/* if (mdp -> fd < 0)*/
-/* {*/
-/* if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0)*/
-/* {*/
-/* return (NULL);*/
-/* }*/
-/* else*/
-/* {*/
-/* mdp -> flags |= MMALLOC_DEVZERO;*/
-/* }*/
-/* }*/
-
/* Now try to map in the first page, copy the malloc descriptor structure
there, and arrange to return a pointer to this new copy. If the mapping
fails, then close the file descriptor if it was opened by us, and arrange
to return a NULL. */
- if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
- {
- memcpy (mbase, mdp, sizeof (mtemp));
-// mdp = (struct mdesc *) mbase;
- }
- else
- {
+ if ((mbase = mdp->morecore(mdp, sizeof(mtemp))) != NULL) {
+ memcpy(mbase, mdp, sizeof(mtemp));
+ } else {
abort();
-// mdp = NULL;
}
-
- return ((PTR) mbase);
+ mdp = (struct mdesc *) mbase;
+
+ { /* create the mutex within that heap */
+ void *old_heap = mmalloc_get_current_heap();
+ mmalloc_set_current_heap(mbase);
+ mdp->mutex = xbt_os_mutex_init();
+ mmalloc_set_current_heap(old_heap);
+ }
+
+ return ((void *) mbase);
}
/* Given an valid file descriptor on an open file, test to see if that file
Returns a pointer to the malloc descriptor if successful, or NULL if
unsuccessful for some reason. */
-static struct mdesc *
-reuse (fd)
- int fd;
+static struct mdesc *reuse(int fd)
{
struct mdesc mtemp;
struct mdesc *mdp = NULL;
- if (lseek (fd, 0L, SEEK_SET) != 0)
+ if (lseek(fd, 0L, SEEK_SET) != 0)
return NULL;
- if (read (fd, (char *) &mtemp, sizeof (mtemp)) != sizeof (mtemp))
+ if (read(fd, (char *) &mtemp, sizeof(mtemp)) != sizeof(mtemp))
return NULL;
- if (mtemp.headersize != sizeof (mtemp))
+ if (mtemp.headersize != sizeof(mtemp))
return NULL;
- if (strcmp (mtemp.magic, MMALLOC_MAGIC) != 0)
+ if (strcmp(mtemp.magic, MMALLOC_MAGIC) != 0)
return NULL;
if (mtemp.version > MMALLOC_VERSION)
return NULL;
mtemp.fd = fd;
- if (__mmalloc_remap_core (&mtemp) == mtemp.base)
- {
+ if (__mmalloc_remap_core(&mtemp) == mtemp.base) {
mdp = (struct mdesc *) mtemp.base;
- mdp -> fd = fd;
- mdp -> morecore = __mmalloc_mmap_morecore;
- if (mdp -> mfree_hook != NULL)
- {
- mmcheckf ((PTR) mdp, (void (*) PARAMS ((void))) NULL, 1);
- }
+ mdp->fd = fd;
+ mdp->morecore = __mmalloc_mmap_morecore;
+ mdp->mutex = xbt_os_mutex_init();
+ if (mdp->mfree_hook != NULL) {
+ mmcheckf((void *) mdp, (void (*)(void)) NULL, 1);
+ }
}
- return (mdp);
-}
-
-#else /* !defined (HAVE_MMAP) */
-/* For systems without mmap, the library still supplies an entry point
- to link to, but trying to initialize access to an mmap'd managed region
- always fails. */
+ { /* create the mutex within that heap */
+ void *old_heap = mmalloc_get_current_heap();
+ mmalloc_set_current_heap(mdp);
+ mdp->mutex = xbt_os_mutex_init();
+ mmalloc_set_current_heap(old_heap);
+ }
-/* ARGSUSED */
-PTR
-mmalloc_attach (fd, baseaddr)
- int fd;
- PTR baseaddr;
-{
- return (NULL);
+ return (mdp);
}
-
-#endif /* defined (HAVE_MMAP) */
-