Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1547)

Unified Diff: mojo/public/c/bindings/lib/buffer.c

Issue 2062323002: Mojo C bindings library pt1: MojomBuffer (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fix rounding on large ints. more unittests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/c/bindings/lib/buffer.c
diff --git a/mojo/public/c/bindings/lib/buffer.c b/mojo/public/c/bindings/lib/buffer.c
new file mode 100644
index 0000000000000000000000000000000000000000..c35fbb3a98a156b4e1154a4b04d67dd3d0b828dc
--- /dev/null
+++ b/mojo/public/c/bindings/lib/buffer.c
@@ -0,0 +1,24 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/public/c/bindings/buffer.h"
+
+#include <assert.h>
+#include <stddef.h> // for NULL
+
+void* MojomBuffer_Allocate(struct MojomBuffer* buf, uint32_t num_bytes) {
+ assert(buf);
+
+ static const size_t kAlignment = 8;
+ const uint32_t bytes_used = buf->num_bytes_used;
+
+ // size = num_bytes is rounded to 8 bytes:
+ const uint64_t size = ((uint64_t)num_bytes + (kAlignment-1))
+ & ~(kAlignment-1);
+ if (bytes_used + size > buf->buf_size || size == 0)
+ return NULL;
+
+ buf->num_bytes_used += size;
+ return buf->buf + bytes_used;
+}

Powered by Google App Engine
This is Rietveld 408576698