File: shmalloc.c

package info (click to toggle)
brutefir 1.0k-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,424 kB
  • sloc: ansic: 22,770; asm: 498; makefile: 322; sh: 6
file content (113 lines) | stat: -rw-r--r-- 2,625 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * (c) Copyright 2001 - 2004 -- Anders Torger
 *
 * This program is open source. For license terms, see the LICENSE file.
 *
 */
#include "defs.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#ifdef __OS_LINUX__
#include <malloc.h>
#else
#include <stddef.h>
#endif

#include "shmalloc.h"

static void
print_shmget_error(size_t size)
{
    switch (errno) {
    case EINVAL:
        fprintf(stderr, "shmget() failed when requesting %d bytes.\n"
                "  The OS shmmax parameter must be increased.\n", (int)size);
        break;
    case ENOSPC:
        fprintf(stderr, "shmget() failed due to exceeded id limit.\n"
                "  The OS shmmni parameter must be increased.\n");
        break;
    case ENOMEM:
        fprintf(stderr, "shmget() failed when requesting %d bytes.\n"
                "Out of memory, or the OS parameters shmmax and/or shmmni "
                "may be too small.\n", (int)size);
        break;
    default:
        fprintf(stderr, "shmget() failed when requesting %d bytes: %s.\n",
                (int)size, strerror(errno));
        break;
    }
}

void *
shmalloc(size_t size)
{
    struct shmid_ds shmid_ds;
    void *p;
    int n;

    if ((n = shmget(IPC_PRIVATE, size, IPC_CREAT | SHM_R | SHM_W)) == -1) {
        print_shmget_error(size);
        return NULL;
    }
    memset(&shmid_ds, 0, sizeof(shmid_ds)); /* to get rid of valgrind warning */
    if ((p = shmat(n, 0, 0)) == (char *)-1 ||
        shmctl(n, IPC_RMID, &shmid_ds) == -1)
    {
	return NULL;
    }
    if (((ptrdiff_t)p & (ALIGNMENT - 1)) != 0) {
	fprintf(stderr, "alignment error\n");
    }
    return p;	
}

void *
shmalloc_id(int *shmid,
            size_t size)
{
    static key_t key = 1;
    struct shmid_ds shmid_ds;
    void *p;
    int n;

    if (shmid != NULL) {
        *shmid = -1;
    }
    
    while ((n = shmget(key, size, IPC_CREAT | IPC_EXCL |
                       SHM_R | SHM_W)) == -1 && errno == EEXIST)
    {
        key++;
    }
    if (n == -1) {
        print_shmget_error(size);
	return NULL;
    }
    
    if  ((p = shmat(n, 0, 0)) == (char *)-1) {
	fprintf(stderr, "Failed to attach to shared memory (shmid %d): %s.\n",
                n, strerror(errno));
	return NULL;
    }
    memset(&shmid_ds, 0, sizeof(shmid_ds));
    if (shmctl(n, IPC_RMID, &shmid_ds) == -1) {
	fprintf(stderr, "Failed to set IPC_RMID (shmid %d): %s.\n", n,
                strerror(errno));
	return NULL;
    }
    
    key++;

    if (shmid != NULL) {
        *shmid = n;
    }
    
    return p;
}