diff options
| author | manuel <manuel@mausz.at> | 2020-10-19 00:52:24 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-10-19 00:52:24 +0200 |
| commit | be933ef2241d79558f91796cc5b3a161f72ebf9c (patch) | |
| tree | fe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/auto_buffer.cpp | |
| parent | 5f8335c1e49ce108ef3481863833c98efa00411b (diff) | |
| download | kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2 kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip | |
sync with upstream
Diffstat (limited to 'xbmc/utils/auto_buffer.cpp')
| -rw-r--r-- | xbmc/utils/auto_buffer.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/xbmc/utils/auto_buffer.cpp b/xbmc/utils/auto_buffer.cpp new file mode 100644 index 0000000..e88a960 --- /dev/null +++ b/xbmc/utils/auto_buffer.cpp | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2013-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "auto_buffer.h" | ||
| 10 | |||
| 11 | #include <new> // for std::bad_alloc | ||
| 12 | #include <stdlib.h> // for malloc(), realloc() and free() | ||
| 13 | |||
| 14 | using namespace XUTILS; | ||
| 15 | |||
| 16 | auto_buffer::auto_buffer(size_t size) | ||
| 17 | { | ||
| 18 | if (!size) | ||
| 19 | return; | ||
| 20 | |||
| 21 | p = malloc(size); // "malloc()" instead of "new" allow to use "realloc()" | ||
| 22 | if (!p) | ||
| 23 | throw std::bad_alloc(); | ||
| 24 | s = size; | ||
| 25 | } | ||
| 26 | |||
| 27 | auto_buffer::~auto_buffer() | ||
| 28 | { | ||
| 29 | free(p); | ||
| 30 | } | ||
| 31 | |||
| 32 | auto_buffer& auto_buffer::allocate(size_t size) | ||
| 33 | { | ||
| 34 | clear(); | ||
| 35 | if (size) | ||
| 36 | { | ||
| 37 | p = malloc(size); | ||
| 38 | if (!p) | ||
| 39 | throw std::bad_alloc(); | ||
| 40 | s = size; | ||
| 41 | } | ||
| 42 | return *this; | ||
| 43 | } | ||
| 44 | |||
| 45 | auto_buffer& auto_buffer::resize(size_t newSize) | ||
| 46 | { | ||
| 47 | if (!newSize) | ||
| 48 | return clear(); | ||
| 49 | |||
| 50 | void* newPtr = realloc(p, newSize); | ||
| 51 | if (!newPtr) | ||
| 52 | throw std::bad_alloc(); | ||
| 53 | p = newPtr; | ||
| 54 | s = newSize; | ||
| 55 | return *this; | ||
| 56 | } | ||
| 57 | |||
| 58 | auto_buffer& auto_buffer::clear(void) | ||
| 59 | { | ||
| 60 | free(p); | ||
| 61 | p = 0; | ||
| 62 | s = 0; | ||
| 63 | return *this; | ||
| 64 | } | ||
| 65 | |||
| 66 | auto_buffer& auto_buffer::attach(void* pointer, size_t size) | ||
| 67 | { | ||
| 68 | clear(); | ||
| 69 | if ((pointer && size) || (!pointer && !size)) | ||
| 70 | { | ||
| 71 | p = pointer; | ||
| 72 | s = size; | ||
| 73 | } | ||
| 74 | return *this; | ||
| 75 | } | ||
| 76 | |||
| 77 | void* auto_buffer::detach(void) | ||
| 78 | { | ||
| 79 | void* returnPtr = p; | ||
| 80 | p = 0; | ||
| 81 | s = 0; | ||
| 82 | return returnPtr; | ||
| 83 | } | ||
| 84 | |||
