1 /* 2 * Copyright (C) the libgit2 contributors. All rights reserved. 3 * 4 * This file is part of libgit2, distributed under the GNU GPL v2 with 5 * a Linking Exception. For full terms see the included COPYING file. 6 */ 7 /** 8 * License: GPL-2.0(Linking Exception) 9 */ 10 module libgit2.buffer; 11 12 13 private import libgit2.common: GIT_EXTERN; 14 15 /* 16 * @file git2/buffer.h 17 * @brief Buffer export structure 18 * 19 * @ingroup Git 20 * @{ 21 */ 22 extern (C): 23 nothrow @nogc: 24 public: 25 26 /** 27 * A data buffer for exporting data from libgit2 28 * 29 * Sometimes libgit2 wants to return an allocated data buffer to the 30 * caller and have the caller take responsibility for freeing that memory. 31 * To make ownership clear in these cases, libgit2 uses `git_buf` to 32 * return this data. Callers should use `git_buf_dispose()` to release 33 * the memory when they are done. 34 * 35 * A `git_buf` contains a pointer to a null-terminated C string, and 36 * the length of the string (not including the null terminator). 37 */ 38 struct git_buf 39 { 40 /** 41 * The buffer contents. `ptr` points to the start of the buffer 42 * being returned. The buffer's length (in bytes) is specified 43 * by the `size` member of the structure, and contains a null 44 * terminator at position `(size + 1)`. 45 */ 46 char* ptr_; 47 48 /** 49 * This field is reserved and unused. 50 */ 51 size_t reserved; 52 53 /** 54 * The length (in bytes) of the buffer pointed to by `ptr`, 55 * not including a null terminator. 56 */ 57 size_t size; 58 } 59 60 /** 61 * Use to initialize a `git_buf` before passing it to a function that 62 * will populate it. 63 */ 64 pragma(inline, true) 65 pure nothrow @safe @nogc @live 66 .git_buf GIT_BUF_INIT() 67 68 do 69 { 70 .git_buf OUTPUT = 71 { 72 ptr_: null, 73 reserved: 0, 74 size: 0, 75 }; 76 77 return OUTPUT; 78 } 79 80 /** 81 * Free the memory referred to by the git_buf. 82 * 83 * Note that this does not free the `git_buf` itself, just the memory 84 * pointed to by `buffer->ptr`. 85 */ 86 @GIT_EXTERN 87 void git_buf_dispose(.git_buf* buffer); 88 89 /* @} */