Skip to content

Instantly share code, notes, and snippets.

@kirs
Created December 1, 2016 17:06
Show Gist options
  • Save kirs/08eed4f04d0181438d460a7c270bae42 to your computer and use it in GitHub Desktop.
Save kirs/08eed4f04d0181438d460a7c270bae42 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <strings.h>
#include "msgpack.h"
#define MSGMAX 256
#define STRING_AND_SIZE(s) (s), (sizeof((s)) - 1)
char vestige_buf[MSGMAX];
char *vestige_cur = vestige_buf;
char *vestige_lim = vestige_buf + sizeof(vestige_buf);
void flush_buffer() {
printf("flushing %ld bytes\n", vestige_cur - vestige_buf);
bzero(vestige_buf, sizeof(vestige_buf));
vestige_cur = vestige_buf;
}
void append_with_fallback(char* (*writer_func)(char*, char*)) {
char *orig_cur = vestige_cur;
vestige_cur = (*writer_func)(vestige_cur, vestige_lim);
if(vestige_cur == 0) {
// need to flush and write again
vestige_cur = orig_cur;
flush_buffer();
vestige_cur = (*writer_func)(vestige_cur, vestige_lim);
printf("successfully written %zu after flush flush\n", vestige_cur - vestige_buf);
} else {
printf("successfully written %zu\n", vestige_cur - orig_cur);
}
}
char *
msgpack_start_span(char *dst, char *lim) {
dst = msgpack_map(dst, lim, 1);
if(dst == 0) return 0;
dst = msgpack_string(dst, lim, STRING_AND_SIZE("type"));
if(dst == 0) return 0;
dst = msgpack_string(dst, lim, STRING_AND_SIZE("start"));
return dst;
}
char *
msgpack_end_span(char *dst, char *lim) {
dst = msgpack_map(dst, lim, 1);
if(dst == 0) return 0;
dst = msgpack_string(dst, lim, STRING_AND_SIZE("type"));
if(dst == 0) return 0;
dst = msgpack_string(dst, lim, STRING_AND_SIZE("end"));
return dst;
}
void start_span() {
append_with_fallback(msgpack_start_span);
}
void end_span() {
append_with_fallback(msgpack_end_span);
}
int main() {
/* Intializes random number generator */
time_t t;
srand((unsigned) time(&t));
int z = 20;
while (z > 0) {
start_span();
end_span();
z--;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment