File: shm.c

package info (click to toggle)
ust 2.14.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,368 kB
  • sloc: xml: 78,798; ansic: 47,114; sh: 5,806; java: 2,202; makefile: 1,833; python: 727; cpp: 384
file content (56 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download
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
/*
 * SPDX-License-Identifier: LGPL-2.1-only
 *
 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "common/ringbuffer/shm.h"
#include "common/align.h"

#include "tap.h"

#define SHM_PATH "/ust-shm-test"

int main(void)
{
	int shmfd;
	size_t shmsize = LTTNG_UST_PAGE_SIZE * 10;
	struct shm_object_table *table;
	struct shm_object *shmobj;
	struct shm_ref shm_ref;

	plan_tests(5);

	/* Open a zero byte shm fd */
	shmfd = shm_open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	ok(shmfd > 0, "Open a POSIX shm fd");

	/* Create a dummy shm object table to test the allocation function */
	table = shm_object_table_create(1, false);
	ok(table, "Create a shm object table");
	assert(table);

	/* This function sets the initial size of the shm with ftruncate and zeros it */
	shmobj = shm_object_table_alloc(table, shmsize, SHM_OBJECT_SHM, shmfd, -1, false);
	ok(shmobj, "Allocate the shm object table");
	assert(shmobj);

	shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 5);
	ok(shm_ref.index != -1, "Allocate an object in the shm with sufficient space");

	shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 6);
	ok(shm_ref.index == -1, "Allocate an object in the shm with insufficient space");

	/* Cleanup */
	shm_object_table_destroy(table, 1);

	return exit_status();
}