<commit_before>/*
 * Copyright (c) 2011, ETH Zurich.
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached LICENSE file.
 * If you do not find this file, copies can be found by writing to:
 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
 */

#include <stdlib.h>
#include <sys/shm.h>
#include <assert.h>
#include <stdbool.h>
#include <errno.h>
#include <stdio.h>
#include <barrelfish/barrelfish.h>
#include <octopus/init.h>
#include <octopus/capability_storage.h>
#include <vfs/fdtab.h>
#include "posixcompat.h"

struct _shm {
    struct shmid_ds ds;
    bool used;
    key_t key;
    struct capref frame;
};

struct _shmap {
    bool used;
    void *mem;
    int id;
};

#define MAX_SHMS        32

struct _shm shms[MAX_SHMS];
struct _shmap shmaps[MAX_SHMS];

void *shmat(int shmid, const void *shmaddr, int shmflg)
{
    assert(shmid >= 0 && shmid < MAX_SHMS);
    struct _shm *s = &shms[shmid];
    errval_t err;

    struct frame_identity id;
    vregion_flags_t attr;

    int i;

    for(i = 0; i < MAX_SHMS; i++) {
      if(!shmaps[i].used) {
        break;
      }
    }

    if(i == MAX_SHMS) {
        // Out of space for mappings
        errno = ENOSPC;
        return NULL;
    }

    struct _shmap *m = &shmaps[i];

    err = invoke_frame_identify(s->frame, &id);
    if(err_is_fail(err)) {
        USER_PANIC_ERR(err, "invoke_frame_identify");
    }

    if(shmflg & SHM_RDONLY) {
        attr = VREGION_FLAGS_READ;
    } else {
        attr = VREGION_FLAGS_READ_WRITE;
    }

    if(shmaddr != NULL) {
        err = vspace_map_one_frame_fixed_attr((lvaddr_t)shmaddr, 1 << id.bits,
                                              s->frame, attr, NULL, NULL);
        m->mem = (void *)shmaddr;
    } else {
        err = vspace_map_one_frame_attr(&m->mem, 1 << id.bits, s->frame,
                                        attr, NULL, NULL);
    }

    if(err_is_fail(err)) {
        DEBUG_ERR(err, "vspace_map_one_frame_(fixed_)attr");
        // XXX: Probably cause it didn't fit the virtual address space
        m->mem = NULL;
        errno = ENOMEM;
        return NULL;
    }

    s->ds.shm_nattch++;
    m->used = true;
    m->id   = shmid;

    POSIXCOMPAT_DEBUG("shmat(%d, %p, %d) = %p\n",
                      shmid, shmaddr, shmflg, s->mem);
    return m->mem;
}

int shmget(key_t key, size_t size, int shmflg)
{
    int i;
    bool newkey = true;

    // XXX: Private key not supported yet
    assert(key != IPC_PRIVATE);

    POSIXCOMPAT_DEBUG("key is not ipc_private\n");
    for(i = 0; i < MAX_SHMS; i++) {
        if(shms[i].used && shms[i].key == key) {
            newkey = false;
            break;
        }
    }<commit_msg>Fix: libposixcompat did not compile with POSIXCOMPAT_DEBUG enabled.<commit_after>/*
 * Copyright (c) 2011, ETH Zurich.
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached LICENSE file.
 * If you do not find this file, copies can be found by writing to:
 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
 */

#include <stdlib.h>
#include <sys/shm.h>
#include <assert.h>
#include <stdbool.h>
#include <errno.h>
#include <stdio.h>
#include <barrelfish/barrelfish.h>
#include <octopus/init.h>
#include <octopus/capability_storage.h>
#include <vfs/fdtab.h>
#include "posixcompat.h"

struct _shm {
    struct shmid_ds ds;
    bool used;
    key_t key;
    struct capref frame;
};

struct _shmap {
    bool used;
    void *mem;
    int id;
};

#define MAX_SHMS        32

struct _shm shms[MAX_SHMS];
struct _shmap shmaps[MAX_SHMS];

void *shmat(int shmid, const void *shmaddr, int shmflg)
{
    assert(shmid >= 0 && shmid < MAX_SHMS);
    struct _shm *s = &shms[shmid];
    errval_t err;

    struct frame_identity id;
    vregion_flags_t attr;

    int i;

    for(i = 0; i < MAX_SHMS; i++) {
      if(!shmaps[i].used) {
        break;
      }
    }

    if(i == MAX_SHMS) {
        // Out of space for mappings
        errno = ENOSPC;
        return NULL;
    }

    struct _shmap *m = &shmaps[i];

    err = invoke_frame_identify(s->frame, &id);
    if(err_is_fail(err)) {
        USER_PANIC_ERR(err, "invoke_frame_identify");
    }

    if(shmflg & SHM_RDONLY) {
        attr = VREGION_FLAGS_READ;
    } else {
        attr = VREGION_FLAGS_READ_WRITE;
    }

    if(shmaddr != NULL) {
        err = vspace_map_one_frame_fixed_attr((lvaddr_t)shmaddr, 1 << id.bits,
                                              s->frame, attr, NULL, NULL);
        m->mem = (void *)shmaddr;
    } else {
        err = vspace_map_one_frame_attr(&m->mem, 1 << id.bits, s->frame,
                                        attr, NULL, NULL);
    }

    if(err_is_fail(err)) {
        DEBUG_ERR(err, "vspace_map_one_frame_(fixed_)attr");
        // XXX: Probably cause it didn't fit the virtual address space
        m->mem = NULL;
        errno = ENOMEM;
        return NULL;
    }

    s->ds.shm_nattch++;
    m->used = true;
    m->id   = shmid;

    POSIXCOMPAT_DEBUG("shmat(%d, %p, %d) = %p\n",
                      shmid, shmaddr, shmflg, m->mem);
    return m->mem;
}

int shmget(key_t key, size_t size, int shmflg)
{
    int i;
    bool newkey = true;

    // XXX: Private key not supported yet
    assert(key != IPC_PRIVATE);

    POSIXCOMPAT_DEBUG("key is not ipc_private\n");
    for(i = 0; i < MAX_SHMS; i++) {
        if(shms[i].used && shms[i].key == key) {
            newkey = false;
            break;
        }
    }
{'fraction_non_alphanumeric': 0.12837345003646974, 'fraction_numerical': 0.005835156819839533, 'mean_word_length': 2.2245737801293357, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1266836', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2009, 'n_words': 501}