C
Collections
The pieces that other languages put in their standard library and C
doesn't have: Vectors, hash maps, ordered maps, strings, memory pools,
thread-safe queues, a thread pool, a structured logger, JSON, YAML and an
HTTP client and server. The compiler deduces your element types, with no
void * casts.
- Typed macrosC11
_Genericsees the types you declared, so a map fromchar *tointstays one at every call site. - One style everywhereCreate with
*_construct, release with*_destroy, or let a*_scopedform do it for you. - Multithreading where it helpsQueues, cache, logger, thread pool and HTTP are thread-safe. The containers leave locking to you, since a lock inside each call can't cover a lookup followed by an insert.
- Tested hardValgrind, ASan, UBSan and TSan on several architectures and systems, plus fuzzing of every parser.
A first program
A vector and a hash map, with the types deduced by the compiler and both released at the end of the scope.
#include <ccollections/chashmap.h>
#include <ccollections/cvector.h>
#include <stdio.h>
int main(void) {
/* A vector of ints. */
cvec_construct_scoped(scores, int); // Will be automatically destroyed
cvec_push(scores, 91);
cvec_push(scores, 74);
cvec_push(scores, 88);
// Using cvec_at for incremental writing
cvec_at(scores, 0) += 2; // 91 -> 93
// Sorting the cvector 'scores' in ascending order.
cvec_sort(scores);
for (size_t i = 0; i < cvec_size(scores); i++)
printf("%d ", cvec_at(scores, i)); // Using cvec_at for reading
printf("\n"); /* 74 88 93 */
/* A hash-map from names to ints; the compiler tracks both the key type and
* the value type. */
chmap_construct_scoped(ages, char *, int); // Will be automatically destroyed
chmap_insert(ages, "alice", 31);
chmap_insert(ages, "bob", 27);
printf("alice is %d\n", chmap_get(ages, "alice"));
if (!chmap_get_ptr(ages, "carol")) {
printf("no carol\n");
}
// In-place modification of an element
*chmap_get_ptr(ages, "bob") += 2; // We know "bob" is present
printf("bob is %d\n", chmap_get(ages, "bob"));
// The cvector and the chashmap will automatically get
// destroyed when the scope for the function main ends.
return 0;
}
Build and install the library, then compile against it with
pkg-config. The install puts the man pages in place too, so
man cvec_push works right after it. It runs on Linux (glibc or
musl), FreeBSD, OpenBSD and macOS, with GCC 10 or Clang 11 and later.
make
sudo make install
gcc -o hello hello.c $(pkg-config --cflags --libs ccollections)
./hello
You don't have to build what you won't use. JSON, YAML, the logger and
the HTTP client and server are each one switch on the make
command line, and turning one off also drops what it needs: OpenSSL goes
with the two HTTP modules, zlib with the logger.
make WITH_CHTTPCLIENT=0 WITH_CHTTPSERVER=0 # no OpenSSL
make WITH_CJSON=0 WITH_CYAML=0 WITH_CLOGGER=0 \
WITH_CHTTPCLIENT=0 WITH_CHTTPSERVER=0 # pthread and libm only
The HTTP modules log through the logger, so it stays in while either of them does. Leaving modules out covers the rest, such as installing a reduced build.
Modules
Each module has an overview page in the manual and a guide with complete examples.
clogger
A structured logger: logfmt, JSON or syslog, with rotation and asynchronous writes
Documentation
- ManualOne page for each public function and macro, with every parameter, return value and edge case.
- Test coverageLine coverage of every source file, merged from all test suites on the main branch.
- ChangelogWhat changed in each release.
- How the library worksBefore any other guide, or when a macro does not behave as you expect
- Building, installing and linkingWhen you install, package or configure the library
- Threads and fork()Before you share data between threads or call
fork() - Custom memory managementWhen you want the library to use your own allocator
- PlatformsTo check which platforms the library runs on
- Compatibility and versioningBefore you upgrade, or to see which guarantees stay stable
- Testing, fuzzing and benchmarksWhen you work on the library itself