Skip to content

Instantly share code, notes, and snippets.

@haberman
Created January 18, 2014 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haberman/8496487 to your computer and use it in GitHub Desktop.
Save haberman/8496487 to your computer and use it in GitHub Desktop.
// Fake upb: a fake implementation for a small subset of the
// actual upb interface, for testing with Rust.
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
uint32_t refcount;
uint32_t number;
} upb_fielddef;
bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number) {
fprintf(stderr, "C: setnumber(%d)\n", number);
f->number = number;
return true;
}
uint32_t upb_fielddef_number(const upb_fielddef *f) {
fprintf(stderr, "C: number() = %d\n", f->number);
return f->number;
}
upb_fielddef *upb_fielddef_new() {
fprintf(stderr, "C: new()\n");
upb_fielddef *f = malloc(sizeof(*f));
f->refcount = 1;
f->number = 1;
return f;
}
void upb_fielddef_ref(upb_fielddef *f) {
fprintf(stderr, "C: ref() = %d -> %d\n", f->refcount, f->refcount + 1);
f->refcount++;
}
void upb_fielddef_unref(upb_fielddef *f) {
fprintf(stderr, "C: unref() = %d -> %d\n", f->refcount, f->refcount - 1);
if (--f->refcount == 0) {
free(f);
}
}
void upb_fielddef_freeze(upb_fielddef *f) {
fprintf(stderr, "C: freeze() = %d\n", f->refcount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment