From cca4818eb7769d6e776bdc30516a5f871f1d6393 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 31 Mar 2012 12:17:32 -0700 Subject: Sync from internal Google development. --- bindings/cpp/upb/bytestream.hpp | 4 ++++ bindings/cpp/upb/def.hpp | 47 +++++++++++++++++++++++++---------------- bindings/linux/Makefile | 20 ++++++++++++++++++ bindings/linux/assert.h | 20 ++++++++++++++++++ bindings/linux/errno.h | 8 +++++++ bindings/linux/stdint.h | 8 +++++++ bindings/linux/stdio.h | 10 +++++++++ bindings/linux/stdlib.h | 22 +++++++++++++++++++ bindings/linux/string.h | 26 +++++++++++++++++++++++ 9 files changed, 147 insertions(+), 18 deletions(-) create mode 100644 bindings/linux/Makefile create mode 100644 bindings/linux/assert.h create mode 100644 bindings/linux/errno.h create mode 100644 bindings/linux/stdint.h create mode 100644 bindings/linux/stdio.h create mode 100644 bindings/linux/stdlib.h create mode 100644 bindings/linux/string.h (limited to 'bindings') diff --git a/bindings/cpp/upb/bytestream.hpp b/bindings/cpp/upb/bytestream.hpp index 8b48690..37d8157 100644 --- a/bindings/cpp/upb/bytestream.hpp +++ b/bindings/cpp/upb/bytestream.hpp @@ -209,6 +209,10 @@ class ByteRegion : public upb_byteregion { uint64_t ofs = start_ofs(); size_t len; const char *ptr = GetPtr(ofs, &len); + // Emperically calling reserve() here is counterproductive and slows down + // benchmarks. If the parsing is happening in a tight loop that is reusing + // the string object, there is probably enough data reserved already and + // the reserve() call is extra overhead. str->assign(ptr, len); ofs += len; while (ofs < end_ofs()) { diff --git a/bindings/cpp/upb/def.hpp b/bindings/cpp/upb/def.hpp index 6998648..6547255 100644 --- a/bindings/cpp/upb/def.hpp +++ b/bindings/cpp/upb/def.hpp @@ -60,12 +60,14 @@ class FieldDef : public upb_fielddef { return static_cast(f); } - static FieldDef* New(void *owner) { return Cast(upb_fielddef_new(owner)); } - FieldDef* Dup(void *owner) const { + static FieldDef* New(const void *owner) { + return Cast(upb_fielddef_new(owner)); + } + FieldDef* Dup(const void *owner) const { return Cast(upb_fielddef_dup(this, owner)); } - void Ref(void *owner) { upb_fielddef_ref(this, owner); } - void Unref(void *owner) { upb_fielddef_unref(this, owner); } + void Ref(const void *owner) { upb_fielddef_ref(this, owner); } + void Unref(const void *owner) { upb_fielddef_unref(this, owner); } bool IsMutable() const { return upb_fielddef_ismutable(this); } bool IsFinalized() const { return upb_fielddef_isfinalized(this); } @@ -194,8 +196,8 @@ class Def : public upb_def { return static_cast(def); } - void Ref(void *owner) const { upb_def_ref(this, owner); } - void Unref(void *owner) const { upb_def_unref(this, owner); } + void Ref(const void *owner) const { upb_def_ref(this, owner); } + void Unref(const void *owner) const { upb_def_unref(this, owner); } void set_full_name(const char *name) { upb_def_setfullname(this, name); } void set_full_name(const std::string& name) { @@ -247,8 +249,8 @@ class MessageDef : public upb_msgdef { return Cast(upb_msgdef_dup(this, owner)); } - void Ref(void *owner) const { upb_msgdef_ref(this, owner); } - void Unref(void *owner) const { upb_msgdef_unref(this, owner); } + void Ref(const void *owner) const { upb_msgdef_ref(this, owner); } + void Unref(const void *owner) const { upb_msgdef_unref(this, owner); } // Read accessors -- may be called at any time. @@ -281,11 +283,13 @@ class MessageDef : public upb_msgdef { // be set, and the message may not already contain any field with this name // or number, and this FieldDef may not be part of another message, otherwise // false is returned and the MessageDef is unchanged. - bool AddField(FieldDef* f, void *owner) { return AddFields(&f, 1, owner); } - bool AddFields(FieldDef*const * f, int n, void *owner) { + bool AddField(FieldDef* f, const void *owner) { + return AddFields(&f, 1, owner); + } + bool AddFields(FieldDef*const * f, int n, const void *owner) { return upb_msgdef_addfields(this, (upb_fielddef*const*)f, n, owner); } - bool AddFields(const std::vector& fields, void *owner) { + bool AddFields(const std::vector& fields, const void *owner) { return AddFields(&fields[0], fields.size(), owner); } @@ -344,11 +348,13 @@ class EnumDef : public upb_enumdef { return static_cast(e); } - static EnumDef* New(void *owner) { return Cast(upb_enumdef_new(owner)); } + static EnumDef* New(const void *owner) { return Cast(upb_enumdef_new(owner)); } - void Ref(void *owner) { upb_enumdef_ref(this, owner); } - void Unref(void *owner) { upb_enumdef_unref(this, owner); } - EnumDef* Dup(void *owner) const { return Cast(upb_enumdef_dup(this, owner)); } + void Ref(const void *owner) { upb_enumdef_ref(this, owner); } + void Unref(const void *owner) { upb_enumdef_unref(this, owner); } + EnumDef* Dup(const void *owner) const { + return Cast(upb_enumdef_dup(this, owner)); + } Def* AsDef() { return Def::Cast(UPB_UPCAST(this)); } const Def* AsDef() const { return Def::Cast(UPB_UPCAST(this)); } @@ -397,10 +403,15 @@ class SymbolTable : public upb_symtab { return static_cast(s); } - static SymbolTable* New() { return Cast(upb_symtab_new()); } + static SymbolTable* New(const void *owner) { + return Cast(upb_symtab_new(owner)); + } - void Ref() const { upb_symtab_unref(this); } - void Unref() const { upb_symtab_unref(this); } + void Ref(const void *owner) const { upb_symtab_unref(this, owner); } + void Unref(const void *owner) const { upb_symtab_unref(this, owner); } + void DonateRef(const void *from, const void *to) const { + upb_symtab_donateref(this, from, to); + } // Adds the given defs to the symtab, resolving all symbols. Only one def // per name may be in the list, but defs can replace existing defs in the diff --git a/bindings/linux/Makefile b/bindings/linux/Makefile new file mode 100644 index 0000000..1736b61 --- /dev/null +++ b/bindings/linux/Makefile @@ -0,0 +1,20 @@ +obj-m = upb.o + +upb-objs = \ + ../../upb/upb.o \ + ../../upb/bytestream.o \ + ../../upb/def.o \ + ../../upb/handlers.o \ + ../../upb/table.o \ + ../../upb/refcount.o \ + ../../upb/msg.o \ + +KVERSION = $(shell uname -r) + +ccflags-y := -I$(PWD) -I$(PWD)/../.. -Wno-declaration-after-statement -std=gnu99 + +all: + make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean diff --git a/bindings/linux/assert.h b/bindings/linux/assert.h new file mode 100644 index 0000000..26d8ab6 --- /dev/null +++ b/bindings/linux/assert.h @@ -0,0 +1,20 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2012 Google Inc. See LICENSE for details. + * Author: Josh Haberman + */ + +#include + +#ifndef UPB_LINUX_ASSERT_H +#define UPB_LINUX_ASSERT_H + +#ifdef NDEBUG +#define assert(x) +#else +#define assert(x) \ + if (!(x)) panic("Assertion failed: %s at %s:%d", #x, __FILE__, __LINE__); +#endif + +#endif diff --git a/bindings/linux/errno.h b/bindings/linux/errno.h new file mode 100644 index 0000000..f45d939 --- /dev/null +++ b/bindings/linux/errno.h @@ -0,0 +1,8 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2012 Google Inc. See LICENSE for details. + * Author: Josh Haberman + */ + +#include diff --git a/bindings/linux/stdint.h b/bindings/linux/stdint.h new file mode 100644 index 0000000..2524b23 --- /dev/null +++ b/bindings/linux/stdint.h @@ -0,0 +1,8 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2012 Google Inc. See LICENSE for details. + * Author: Josh Haberman + */ + +#include diff --git a/bindings/linux/stdio.h b/bindings/linux/stdio.h new file mode 100644 index 0000000..72c1b0d --- /dev/null +++ b/bindings/linux/stdio.h @@ -0,0 +1,10 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2012 Google Inc. See LICENSE for details. + * Author: Josh Haberman + * + * Linux-kernel implementations of some stdlib.h functions. + */ + +#include // For sprintf and friends. diff --git a/bindings/linux/stdlib.h b/bindings/linux/stdlib.h new file mode 100644 index 0000000..8381b13 --- /dev/null +++ b/bindings/linux/stdlib.h @@ -0,0 +1,22 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2012 Google Inc. See LICENSE for details. + * Author: Josh Haberman + * + * Linux-kernel implementations of some stdlib.h functions. + */ + +#include + +#ifndef UPB_LINUX_STDLIB_H +#define UPB_LINUX_STDLIB_H + +static inline void *malloc(size_t size) { return kmalloc(size, GFP_ATOMIC); } +static inline void free(void *p) { kfree(p); } + +static inline void *realloc(void *p, size_t size) { + return krealloc(p, size, GFP_ATOMIC); +} + +#endif diff --git a/bindings/linux/string.h b/bindings/linux/string.h new file mode 100644 index 0000000..69de3fa --- /dev/null +++ b/bindings/linux/string.h @@ -0,0 +1,26 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2012 Google Inc. See LICENSE for details. + * Author: Josh Haberman + */ + +#ifndef UPB_LINUX_STRING_H_ +#define UPB_LINUX_STRING_H_ + +#include +#include +#include "upb/upb.h" // For INLINE. + +INLINE char *strdup(const char *s) { + size_t len = strlen(s); + char *ret = malloc(len + 1); + if (ret == NULL) return NULL; + // Be particularly defensive and guard against buffer overflow if there + // is a concurrent mutator. + strncpy(ret, s, len); + ret[len] = '\0'; + return ret; +} + +#endif /* UPB_DEF_H_ */ -- cgit v1.2.3