hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
// Copyright 2020 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
 
static int CommShm(char *name, int projid, int size, int flags)
{
    int retry_cnt = 0;
    int shmid;
    key_t key = ftok(name, projid);
 
    if(key == -1) {
        perror("ftok");
        return -1;
    }
retry:
    shmid = shmget(key, size, flags);
    if(shmid < 0) {
        if (retry_cnt++ < 2) {
            shmid = shmget(key, 0, 0);
            if (shmid != -1) {
                shmctl(shmid, IPC_RMID, 0);
                goto retry;
            }
        }
        perror("shmget");
        return -2;
    }
 
    return shmid;
}
 
int DestroyShm(int shmid)
{
    if(shmctl(shmid, IPC_RMID,NULL) < 0) {
        perror("shmctl");
        return -1;
    }
 
    return 0;
}
 
int CreateShm(char *name, int projid, int size)
{
    return CommShm(name, projid, size, IPC_CREAT | IPC_EXCL | 0666);
}
 
int GetShm(char *name, int projid, int size)
{
    return CommShm(name, projid, size, IPC_CREAT);
}
 
char *Shmat(int shmid)
{
    return shmat(shmid, NULL, 0);
}
 
void Shmdt(char *addr)
{
    shmdt(addr);
}