summaryrefslogtreecommitdiff
path: root/bindings
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2012-03-31 12:17:32 -0700
committerJoshua Haberman <jhaberman@gmail.com>2012-03-31 12:17:32 -0700
commitcca4818eb7769d6e776bdc30516a5f871f1d6393 (patch)
treee67dd65d5c016028ae976b09b2d69f6b7525aa5f /bindings
parent26ed1e996171c8ffa2ced42ac69b1b82c1956e1f (diff)
Sync from internal Google development.
Diffstat (limited to 'bindings')
-rw-r--r--bindings/cpp/upb/bytestream.hpp4
-rw-r--r--bindings/cpp/upb/def.hpp47
-rw-r--r--bindings/linux/Makefile20
-rw-r--r--bindings/linux/assert.h20
-rw-r--r--bindings/linux/errno.h8
-rw-r--r--bindings/linux/stdint.h8
-rw-r--r--bindings/linux/stdio.h10
-rw-r--r--bindings/linux/stdlib.h22
-rw-r--r--bindings/linux/string.h26
9 files changed, 147 insertions, 18 deletions
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<const FieldDef*>(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<const Def*>(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<FieldDef*>& fields, void *owner) {
+ bool AddFields(const std::vector<FieldDef*>& fields, const void *owner) {
return AddFields(&fields[0], fields.size(), owner);
}
@@ -344,11 +348,13 @@ class EnumDef : public upb_enumdef {
return static_cast<const EnumDef*>(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<const SymbolTable*>(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 <jhaberman@gmail.com>
+ */
+
+#include <linux/kernel.h>
+
+#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 <jhaberman@gmail.com>
+ */
+
+#include <linux/errno.h>
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 <jhaberman@gmail.com>
+ */
+
+#include <linux/types.h>
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 <jhaberman@gmail.com>
+ *
+ * Linux-kernel implementations of some stdlib.h functions.
+ */
+
+#include <linux/kernel.h> // 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 <jhaberman@gmail.com>
+ *
+ * Linux-kernel implementations of some stdlib.h functions.
+ */
+
+#include <linux/slab.h>
+
+#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 <jhaberman@gmail.com>
+ */
+
+#ifndef UPB_LINUX_STRING_H_
+#define UPB_LINUX_STRING_H_
+
+#include <linux/string.h>
+#include <stdlib.h>
+#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_ */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback