about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--.clang-format11
-rw-r--r--Makefile19
-rw-r--r--bench.c153
-rw-r--r--cached.c73
-rw-r--r--cached.h18
5 files changed, 274 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..1585e79
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,11 @@
+BasedOnStyle: LLVM
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Linux
+AllowShortIfStatementsOnASingleLine: false
+IndentCaseLabels: false
+
+AlwaysBreakAfterReturnType: All
+AlignAfterOpenBracket: DontAlign
+ContinuationIndentWidth: 16
+SortIncludes: false
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0ff5c08
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,19 @@
+CFLAGS=-std=gnu11 -Wall -Wextra -Wpedantic -Og -g -D_DEFAULT_SOURCE \
+       -fsanitize=undefined -fsanitize=address
+LDFLAGS=-fsanitize=undefined -fsanitize=address
+
+ALL=bench
+
+all: $(ALL)
+
+bench: bench.o cached.o
+
+clean:
+	$(RM) $(ALL) *.o
+
+.PHONY:	all check clean format
+
+check:
+	cppcheck --enable=all --std=c11 *.[ch]
+format:
+	clang-format -i *.[ch]
diff --git a/bench.c b/bench.c
new file mode 100644
index 0000000..fb95c2e
--- /dev/null
+++ b/bench.c
@@ -0,0 +1,153 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+
+#include "cached.h"
+
+#define UNUSED __attribute__((unused))
+
+static sig_atomic_t running;
+
+static void
+panic(const char *msg)
+{
+	fprintf(stderr, "panic: %s\n", msg);
+	exit(EXIT_FAILURE);
+}
+
+static void
+alarm_handler(int sig UNUSED, siginfo_t *info UNUSED, void *arg UNUSED)
+{
+	running = 0;
+}
+
+static void
+trap_signals(void)
+{
+	struct sigaction sa;
+
+	sa.sa_sigaction = alarm_handler;
+	sa.sa_flags = SA_SIGINFO;
+
+	if (sigaction(SIGALRM, &sa, NULL) < 0) {
+		panic("failed to setup signal handler");
+	}
+}
+
+static void
+set_alarm(void)
+{
+	running = 1;
+	alarm(1);
+}
+
+static size_t
+bench_malloc(size_t allocs, size_t objsize)
+{
+	void *objs[allocs];
+	size_t count = 0;
+
+	while (running) {
+		for (size_t i = 0; i < allocs; i++) {
+			objs[i] = calloc(1, objsize);
+			count++;
+		}
+		for (size_t i = 0; i < allocs; i++) {
+			free(objs[i]);
+		}
+	}
+
+	return count;
+}
+
+static size_t
+bench_ca_alloc(size_t allocs)
+{
+	void *objs[allocs];
+	size_t count = 0;
+
+	while (running) {
+		for (size_t i = 0; i < allocs; i++) {
+			objs[i] = ca_alloc();
+			count++;
+		}
+		for (size_t i = 0; i < allocs; i++) {
+			ca_free(objs[i]);
+		}
+	}
+
+	return count;
+}
+
+static void
+bench_four(void)
+{
+	if (ca_setup(256, 4096) < 0) {
+		panic("failed to setup cached allocator");
+	}
+
+	set_alarm();
+
+	size_t allocs = bench_ca_alloc(1024);
+
+	printf("%-12s\t%9zu allocs/second\n", "huge_cache", allocs);
+
+	ca_cleanup();
+}
+
+static void
+bench_one(void)
+{
+	if (ca_setup(256, 1024) < 0) {
+		panic("failed to setup cached allocator");
+	}
+
+	set_alarm();
+
+	size_t allocs = bench_ca_alloc(1024);
+
+	printf("%-12s\t%9zu allocs/second\n", "large_cache", allocs);
+
+	ca_cleanup();
+}
+
+static void
+bench_two(void)
+{
+	if (ca_setup(256, 16) < 0) {
+		panic("failed to setup cached allocator");
+	}
+
+	set_alarm();
+
+	size_t allocs = bench_ca_alloc(1024);
+
+	printf("%-12s\t%9zu allocs/second\n", "small_cache", allocs);
+
+	ca_cleanup();
+}
+
+static void
+bench_three(void)
+{
+	set_alarm();
+
+	size_t allocs = bench_malloc(1024, 256);
+
+	printf("%-12s\t%9zu allocs/second\n", "just_malloc", allocs);
+}
+
+int
+main(void)
+{
+	trap_signals();
+
+	bench_three();
+	puts("");
+	bench_two();
+	bench_one();
+	bench_four();
+
+	exit(EXIT_SUCCESS);
+}
diff --git a/cached.c b/cached.c
new file mode 100644
index 0000000..8174cda
--- /dev/null
+++ b/cached.c
@@ -0,0 +1,73 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cached.h"
+
+static size_t object_size;
+static size_t max_cached;
+static size_t num_cached;
+static void **cache;
+
+int
+ca_setup(size_t osize, size_t csize)
+{
+	/* TODO allow osize == 0 following malloc? */
+	if (osize == 0 || csize == 0) {
+		return -1;
+	}
+
+	cache = calloc(csize, sizeof(*cache));
+	if (cache == NULL) {
+		return -2;
+	}
+
+	object_size = osize;
+	max_cached = csize;
+	num_cached = 0;
+
+	return 0;
+}
+
+void *
+ca_alloc(void)
+{
+	assert(cache != NULL);
+
+	if (num_cached > 0) {
+		void *object = cache[--num_cached];
+		return object;
+	}
+
+	return calloc(1, object_size);
+}
+
+void
+ca_free(void *object)
+{
+	assert(cache != NULL);
+
+	if (object == NULL) {
+		/* free(3) also does nothing for NULL */
+		return;
+	}
+
+	if (num_cached < max_cached) {
+		cache[num_cached++] = object;
+		/* memset here to catch dangling references */
+		memset(object, 0, object_size);
+		return;
+	}
+
+	free(object);
+}
+
+void
+ca_cleanup(void)
+{
+	for (size_t i = 0; i < num_cached; i++) {
+		free(cache[i]);
+	}
+	free(cache);
+	cache = NULL;
+}
diff --git a/cached.h b/cached.h
new file mode 100644
index 0000000..32ca61c
--- /dev/null
+++ b/cached.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#ifndef CACHED_H_
+#define CACHED_H_
+
+int
+ca_setup(size_t object_size, size_t cache_size);
+
+void *
+ca_alloc(void);
+
+void
+ca_free(void *object);
+
+void
+ca_cleanup(void);
+
+#endif
68' href='#n468'>468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689