Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <sys/types.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
// Posix: If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free()
// glibc: If n is zero, malloc returns a minumum-sized chunk.
// So for glibc we don't need to special-case len==0.
static void *malloc_nofail(size_t size) {
char *p = malloc(size);
if (p == NULL) {
fprintf(stderr, "%m\n");
_exit(1);
}
return p;
}
static void *malloc_nofail_debug(size_t len) {
void *p = malloc_nofail(len);
printf("XXX malloc(%lu) -> %p capacity %lu\n", len, p, malloc_usable_size(p));
return p;
}
#define malloc_nofail malloc_nofail_debug
// Posix: If ptr is NULL, then the call is equivalent to malloc(size), for all values of size;
// if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr).
//
void *realloc_nofail(void *ptr, size_t size) {
void *p = realloc(ptr, size);
if (p == NULL && size) {
fprintf(stderr, "%m\n");
_exit(1);
}
return p;
}
static void *realloc_nofail_debug(void *ptr, size_t size) {
size_t old_capacity = malloc_usable_size(ptr);
void *old = ptr;
ptr = realloc_nofail(ptr,size);
char *marker = ( ptr == old ? "" : " <-- move");
printf("XXX realloced %p size %lu req %lu to %p size %lu%s\n", old, old_capacity, size, ptr, malloc_usable_size(ptr), marker);
return ptr;
}
#define realloc_nofail realloc_nofail_debug
static void *memdup_nofail(void *ptr, size_t len) {
void *p = malloc_nofail(len);
memcpy(p, ptr, len);
return p;
}
/************************************************************/
typedef struct {
size_t len;
char *data;
} string;
string string_from_cstr(char *c) {
string s = {strlen(c), c};
return s;
}
typedef struct {
string s;
} dstring;
/************************************************************/
static dstring dstring_new(void) {
dstring dstr = { {0, NULL} };
return dstr;
}
static dstring dstring_from_string(string *str) {
dstring dstr = { { str->len, memdup_nofail(str->data, str->len) }};
return dstr;
}
static void dstring_append_cstr(dstring *dstr, char *cstr) {
size_t l = strlen(cstr);
dstr->s.data = realloc_nofail(dstr->s.data, dstr->s.len + l);
memcpy(&dstr->s.data[dstr->s.len], cstr, l);
dstr->s.len += l;
}
static void dstring_append_string(dstring *dstr, string string) {
dstr->s.data = realloc_nofail(dstr->s.data, dstr->s.len + string.len);
memcpy(&dstr->s.data[dstr->s.len], string.data, string.len);
dstr->s.len += string.len;
}
static void dstring_set_len(dstring *dstr, size_t len) {
dstr->s.data = realloc_nofail(dstr->s.data, len);
dstr->s.len = len;
}
static void dstring_free(dstring dstr) {
printf("XXX dstring_free(%p) free %p\n", &dstr, dstr.s.data);
free(dstr.s.data);
}
static void dstring_cleanup(dstring *dstr) {
dstring_free(*dstr);
}
/************************************************************/
int main() {
char *x = malloc_nofail(0);
printf("x: %p\n", x);
char *y = malloc_nofail(0);
printf("y: %p\n", y);
x = realloc_nofail(x, 10);
y = realloc_nofail(y, 10);
free(x);
free(y);
string donald = string_from_cstr("Donald");
__attribute__ ((__cleanup__(dstring_cleanup))) dstring s = dstring_new();
dstring_append_cstr(&s, "Hallo, ");
dstring_append_string(&s, donald);
dstring_append_cstr(&s, " dies ");
dstring_append_cstr(&s, "dies ");
dstring_append_cstr(&s, "ist ");
dstring_append_cstr(&s, "ein ");
dstring_append_cstr(&s, "Test.");
printf("%.*s\n", (int)s.s.len, s.s.data);
__attribute__ ((__cleanup__(dstring_cleanup))) dstring d1 = dstring_new();
__attribute__ ((__cleanup__(dstring_cleanup))) dstring d2 = dstring_new();
for (int i = 0 ; i < 1000 ; i++) {
dstring_append_cstr(&d1, "x");
dstring_append_string(&d2, d1.s);
}
for (size_t i=1000 ; ; i--) {
dstring_set_len(&d1, i);
if (i == 0)
break;
}
printf("done\n");
}