Skip to content

Commit b21b1f7

Browse files
author
Andy Rudoff
committed
cleanup: simpler, smaller, uniformity, code style
1 parent 43e9b77 commit b21b1f7

File tree

7 files changed

+176
-292
lines changed

7 files changed

+176
-292
lines changed

chapter10/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pmem_allocator
2+
pmem_config
3+
pmem_detect_kind
4+
vector_of_strings
5+
vmemcache

chapter10/Makefile

+9-7
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31-
CC = gcc
32-
CXX = g++
33-
RM = rm -f
31+
#
32+
# Makefile for chapter10 examples
33+
#
3434

3535
TARGETS = pmem_allocator vector_of_strings pmem_config pmem_detect_kind vmemcache
3636
TARGETS_LISTINGS = $(addsuffix .lst, $(TARGETS))
@@ -39,12 +39,10 @@ all: $(TARGETS) listings
3939
listings: $(TARGETS_LISTINGS)
4040

4141
%.lst: %.cpp
42-
sed -i 's/\t/ /g' $^
43-
cat -n $^ > $@
42+
expand -t 4 < $^ | cat -n > $@
4443

4544
%.lst: %.c
46-
sed -i 's/\t/ /g' $^
47-
cat -n $^ > $@
45+
expand -t 4 < $^ | cat -n > $@
4846

4947
%: %.cpp
5048
$(CXX) -g -o $@ $< -lmemkind -std=c++11
@@ -53,5 +51,9 @@ listings: $(TARGETS_LISTINGS)
5351
$(CC) -g -o $@ $< -lmemkind -lvmemcache
5452

5553
clean:
54+
$(RM) *.o core a.out
55+
56+
clobber: clean
5657
$(RM) $(TARGETS) $(TARGETS_LISTINGS)
5758

59+
.PHONY: all clean clobber listings

chapter10/pmem_allocator.cpp

+20-21
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,30 @@
3131
*/
3232

3333
/*
34-
* pmem_allocator.cpp - Demonstrates using the
35-
* pmem::allocator
36-
* with std:vector.
34+
* pmem_allocator.cpp - pmem::allocator with std:vector
3735
*/
3836

3937
#include <pmem_allocator.h>
4038
#include <vector>
4139
#include <cassert>
42-
40+
4341
int main(int argc, char *argv[]) {
44-
const size_t pmem_max_size = 64*1024*1024; //64 MB
45-
const std::string pmem_dir("/pmemfs/");
46-
47-
// Create allocator object
48-
libmemkind::pmem::allocator<int> alc(pmem_dir,
49-
pmem_max_size);
50-
// Create std::vector with our allocator.
51-
std::vector<int, libmemkind::pmem::allocator<int>
52-
> v(alc);
53-
54-
for(int i = 0; i < 100; ++i)
55-
v.push_back(i);
56-
57-
for(int i = 0; i < 100; ++i)
58-
assert(v[i] == i);
59-
60-
return 0;
42+
const size_t pmem_max_size = 64 * 1024 * 1024; //64 MB
43+
const std::string pmem_dir("/daxfs");
44+
45+
// Create allocator object
46+
libmemkind::pmem::allocator<int>
47+
alc(pmem_dir, pmem_max_size);
48+
49+
// Create std::vector with our allocator.
50+
std::vector<int,
51+
libmemkind::pmem::allocator<int>> v(alc);
52+
53+
for (int i = 0; i < 100; ++i)
54+
v.push_back(i);
55+
56+
for (int i = 0; i < 100; ++i)
57+
assert(v[i] == i);
58+
59+
exit(0);
6160
}

chapter10/pmem_config.c

+53-67
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
*/
3232

33-
/*
34-
* pmem_config.c - Demonstrates the use of several
35-
* configuration functions within
36-
* libmemkind.
37-
*/
33+
/*
34+
* pmem_config.c - demonstrate configuration functions
35+
*/
3836

3937
#include <memkind.h>
4038

@@ -44,72 +42,60 @@
4442

4543
#define PMEM_MAX_SIZE (1024 * 1024 * 32)
4644

47-
static char path[PATH_MAX] = "pmemfs//";
45+
char path[PATH_MAX] = "/daxfs";
4846

49-
static void print_err_message(int err)
47+
void memkind_fatal(int err)
5048
{
51-
char error_message[MEMKIND_ERROR_MESSAGE_SIZE];
52-
memkind_error_message(err, error_message,
53-
MEMKIND_ERROR_MESSAGE_SIZE);
54-
fprintf(stderr, "%s\n", error_message);
49+
char error_message[MEMKIND_ERROR_MESSAGE_SIZE];
50+
51+
memkind_error_message(err, error_message,
52+
MEMKIND_ERROR_MESSAGE_SIZE);
53+
fprintf(stderr, "%s\n", error_message);
54+
exit(1);
5555
}
5656

5757
int main(int argc, char *argv[])
5858
{
59-
struct memkind *pmem_kind = NULL;
60-
int err = 0;
61-
62-
if (argc > 2) {
63-
fprintf(stderr,
64-
"Usage: %s [pmem_kind_dir_path]\n",
65-
argv[0]);
66-
return 1;
67-
} else if (argc == 2 &&
68-
(realpath(argv[1], path) == NULL)) {
69-
fprintf(stderr,
70-
"Incorrect pmem_kind_dir_path %s\n",
71-
argv[1]);
72-
return 1;
73-
}
74-
75-
fprintf(stdout,
76-
"This example shows how to use custom "
77-
"configuration to create pmem kind."
78-
"\nPMEM kind directory: %s\n", path);
79-
80-
struct memkind_config *test_cfg =
81-
memkind_config_new();
82-
if (!test_cfg) {
83-
fprintf(stderr,
84-
"Unable to create memkind cfg.\n");
85-
return 1;
86-
}
87-
88-
memkind_config_set_path(test_cfg, path);
89-
memkind_config_set_size(test_cfg, PMEM_MAX_SIZE);
90-
memkind_config_set_memory_usage_policy(test_cfg,
91-
MEMKIND_MEM_USAGE_POLICY_CONSERVATIVE);
92-
93-
94-
// Create PMEM partition with the configuration
95-
err = memkind_create_pmem_with_config(test_cfg,
96-
&pmem_kind);
97-
if (err) {
98-
print_err_message(err);
99-
return 1;
100-
}
101-
102-
err = memkind_destroy_kind(pmem_kind);
103-
if (err) {
104-
print_err_message(err);
105-
return 1;
106-
}
107-
108-
memkind_config_delete(test_cfg);
109-
110-
fprintf(stdout,
111-
"PMEM kind and configuration was successfully"
112-
" created and destroyed.\n");
113-
114-
return 0;
59+
struct memkind *pmem_kind;
60+
int err;
61+
62+
if (argc > 2) {
63+
fprintf(stderr,
64+
"Usage: %s [pmem_kind_dir_path]\n",
65+
argv[0]);
66+
exit(1);
67+
} else if (argc == 2 &&
68+
(realpath(argv[1], path) == NULL)) {
69+
perror(argv[1]);
70+
exit(1);
71+
}
72+
73+
struct memkind_config *test_cfg =
74+
memkind_config_new();
75+
if (test_cfg == NULL) {
76+
fprintf(stderr,
77+
"memkind_config_new: out of memory\n");
78+
exit(1);
79+
}
80+
81+
memkind_config_set_path(test_cfg, path);
82+
memkind_config_set_size(test_cfg, PMEM_MAX_SIZE);
83+
memkind_config_set_memory_usage_policy(test_cfg,
84+
MEMKIND_MEM_USAGE_POLICY_CONSERVATIVE);
85+
86+
// Create PMEM partition with the configuration
87+
err = memkind_create_pmem_with_config(test_cfg,
88+
&pmem_kind);
89+
if (err) {
90+
memkind_fatal(err);
91+
}
92+
93+
err = memkind_destroy_kind(pmem_kind);
94+
if (err) {
95+
memkind_fatal(err);
96+
}
97+
98+
memkind_config_delete(test_cfg);
99+
100+
exit(0);
115101
}

0 commit comments

Comments
 (0)