<commit_before>/*
   (c) Copyright 2001  <NAME> <<EMAIL>>
   All rights reserved.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include "shmalloc_internal.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>

#define SH_BASE          0x70000000
#define SH_MAX_SIZE      0x20000000
#define SH_FILE_NAME     "/fusion.shm"
#define SH_DEFAULT_NAME  "/dev/shm" SH_FILE_NAME
#define SH_MOUNTS_FILE   "/proc/mounts"
#define SH_SHMFS_TYPE    "tmpfs"
#define SH_BUFSIZE       256

static int   fd            = -1;
static void *mem           = NULL;
static int   size          = 0;
static char  default_name[]= SH_DEFAULT_NAME;

static char *
shmalloc_check_shmfs (void)
{
     FILE * mounts_handle = NULL;
     char * pointer       = NULL;
     char * mount_point   = NULL;
     char * mount_fs      = NULL;

     char   buffer[SH_BUFSIZE];

     if (!(mounts_handle = fopen (SH_MOUNTS_FILE, "r")))
          return(default_name);

     while (fgets (buffer, SH_BUFSIZE, mounts_handle)) {
          pointer = buffer;
          strsep (&pointer, " ");
          mount_point = strsep (&pointer, " ");
          mount_fs = strsep (&pointer, " ");
          if (mount_fs && mount_point
              && (strlen (mount_fs) == strlen (SH_SHMFS_TYPE))
              && (!(strcmp (mount_fs, SH_SHMFS_TYPE))))
          {
               if (!(pointer = malloc(strlen (mount_point)
                                      + strlen (SH_FILE_NAME) + 1)))
               {
                    fclose (mounts_handle);
                    return(default_name);
               }
               strcpy (pointer, mount_point);
               strcat (pointer, SH_FILE_NAME);
               fclose (mounts_handle);
               return(pointer);
          }
     }

     fclose (mounts_handle);
     return(default_name);
}

void *__shmalloc_init (bool initialize)
{
     struct stat   st;
     char        * sh_name = NULL;

     if (mem)
          return mem;

     /* try to find out where the shmfs is actually mounted */
     sh_name = shmalloc_check_shmfs ();

     /* open the virtual file */
     if (initialize)
          fd = open (sh_name, O_RDWR | O_CREAT, 0660);
     else
          fd = open (sh_name, O_RDWR);
     if (fd < 0) {
          perror ("opening shared memory file");
          return NULL;
     }

     /* init or join */
     if (initialize) {
          size = sizeof(shmalloc_heap);
          ftruncate (fd, size);
     }
     else {
          /* query size of memory */
          if (fstat (fd, &st) < 0) {
               perror ("fstating shared memory file");
               close (fd);
               fd = -1;
               return NULL;
          }

          size = st.st_size;
     }

     /* map it shared */
     mem = mmap ((void*) SH_BASE, size,
                 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
     if (mem == MAP_FAILED) {
          perror ("mmapping shared memory file");
          close (fd);
          fd  = -1;
          mem = NULL;
     }
<commit_msg>Do chmod after creation because of umask.<commit_after>/*
   (c) Copyright 2001  <NAME> <<EMAIL>>
   All rights reserved.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include "shmalloc_internal.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>

#define SH_BASE          0x70000000
#define SH_MAX_SIZE      0x20000000
#define SH_FILE_NAME     "/fusion.shm"
#define SH_DEFAULT_NAME  "/dev/shm" SH_FILE_NAME
#define SH_MOUNTS_FILE   "/proc/mounts"
#define SH_SHMFS_TYPE    "tmpfs"
#define SH_BUFSIZE       256

static int   fd            = -1;
static void *mem           = NULL;
static int   size          = 0;
static char  default_name[]= SH_DEFAULT_NAME;

static char *
shmalloc_check_shmfs (void)
{
     FILE * mounts_handle = NULL;
     char * pointer       = NULL;
     char * mount_point   = NULL;
     char * mount_fs      = NULL;

     char   buffer[SH_BUFSIZE];

     if (!(mounts_handle = fopen (SH_MOUNTS_FILE, "r")))
          return(default_name);

     while (fgets (buffer, SH_BUFSIZE, mounts_handle)) {
          pointer = buffer;
          strsep (&pointer, " ");
          mount_point = strsep (&pointer, " ");
          mount_fs = strsep (&pointer, " ");
          if (mount_fs && mount_point
              && (strlen (mount_fs) == strlen (SH_SHMFS_TYPE))
              && (!(strcmp (mount_fs, SH_SHMFS_TYPE))))
          {
               if (!(pointer = malloc(strlen (mount_point)
                                      + strlen (SH_FILE_NAME) + 1)))
               {
                    fclose (mounts_handle);
                    return(default_name);
               }
               strcpy (pointer, mount_point);
               strcat (pointer, SH_FILE_NAME);
               fclose (mounts_handle);
               return(pointer);
          }
     }

     fclose (mounts_handle);
     return(default_name);
}

void *__shmalloc_init (bool initialize)
{
     struct stat   st;
     char        * sh_name = NULL;

     if (mem)
          return mem;

     /* try to find out where the shmfs is actually mounted */
     sh_name = shmalloc_check_shmfs ();

     /* open the virtual file */
     if (initialize)
          fd = open (sh_name, O_RDWR | O_CREAT, 0660);
     else
          fd = open (sh_name, O_RDWR);
     if (fd < 0) {
          perror ("opening shared memory file");
          return NULL;
     }

     /* init or join */
     if (initialize) {
          chmod (sh_name, 0660);

          size = sizeof(shmalloc_heap);
          ftruncate (fd, size);
     }
     else {
          /* query size of memory */
          if (fstat (fd, &st) < 0) {
               perror ("fstating shared memory file");
               close (fd);
               fd = -1;
               return NULL;
          }

          size = st.st_size;
     }

     /* map it shared */
     mem = mmap ((void*) SH_BASE, size,
                 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
     if (mem == MAP_FAILED) {
          perror ("mmapping shared memory file");
          close (fd);
          fd  = -1;
          mem = NULL;
     }

{'fraction_non_alphanumeric': 0.09929906542056074, 'fraction_numerical': 0.014018691588785047, 'mean_word_length': 1.925208807896735, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '647231', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2308, 'n_words': 745}