Chromium Code Reviews| 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..68497b710a7dfb083f013ee0f24b9b445885557a |
| --- /dev/null |
| +++ b/mojo/public/c/bindings/lib/buffer.c |
| @@ -0,0 +1,22 @@ |
| +// 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, size_t num_bytes) { |
| + assert(buf); |
| + |
| + static const size_t kAlignment = 8; |
| + const size_t bytes_used = buf->num_bytes_used; |
| + // size = num_bytes aligned to 8 bytes: |
| + const size_t size = (num_bytes + (kAlignment-1)) & ~(kAlignment-1); |
|
viettrungluu
2016/06/15 15:00:58
Note that this will lead to sadness if num_bytes i
vardhan
2016/06/15 15:48:00
oof. i think its better to make it a uint32_t rat
|
| + if (bytes_used + size > buf->buf_size || bytes_used + size <= bytes_used) |
|
viettrungluu
2016/06/15 15:00:57
nit: <= -> <. There's no reason to not allow "allo
vardhan
2016/06/15 15:48:00
Allocations of 0 seem like a programmatic error --
viettrungluu
2016/06/15 15:59:49
It's not necessarily a programmatic error for case
vardhan
2016/06/15 16:10:21
Done.
|
| + return NULL; |
| + |
| + buf->num_bytes_used += size; |
| + return buf->buf + bytes_used; |
| +} |