allocator_std.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2015 Rockchip Electronics Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include "os_mem.h"
  18. #include "mpp_mem.h"
  19. #include "mpp_debug.h"
  20. #include "allocator_std.h"
  21. typedef struct {
  22. size_t alignment;
  23. MppAllocFlagType flags;
  24. RK_S32 fd_count;
  25. } allocator_ctx;
  26. static MPP_RET allocator_std_open(void **ctx, size_t alignment, MppAllocFlagType flags)
  27. {
  28. allocator_ctx *p = NULL;
  29. if (NULL == ctx) {
  30. mpp_err_f("do not accept NULL input\n");
  31. return MPP_ERR_NULL_PTR;
  32. }
  33. mpp_err_f("Warning: std allocator should be used on simulation mode only\n");
  34. p = mpp_malloc(allocator_ctx, 1);
  35. if (p) {
  36. p->alignment = alignment;
  37. p->flags = flags;
  38. p->fd_count = 0;
  39. }
  40. *ctx = p;
  41. return p ? MPP_OK : MPP_NOK;
  42. }
  43. static MPP_RET allocator_std_alloc(void *ctx, MppBufferInfo *info)
  44. {
  45. if (NULL == ctx) {
  46. mpp_err_f("found NULL context input\n");
  47. return MPP_ERR_NULL_PTR;
  48. }
  49. mpp_err_f("Warning: std allocator should be used on simulation mode only\n");
  50. (void)info;
  51. return MPP_NOK;
  52. }
  53. static MPP_RET allocator_std_free(void *ctx, MppBufferInfo *info)
  54. {
  55. (void) ctx;
  56. if (info->ptr)
  57. os_free(info->ptr);
  58. return MPP_OK;
  59. }
  60. static MPP_RET allocator_std_import(void *ctx, MppBufferInfo *info)
  61. {
  62. allocator_ctx *p = (allocator_ctx *)ctx;
  63. mpp_assert(ctx);
  64. mpp_assert(info->ptr);
  65. mpp_assert(info->size);
  66. info->hnd = NULL;
  67. info->fd = p->fd_count++;
  68. return MPP_OK;
  69. }
  70. static MPP_RET allocator_std_release(void *ctx, MppBufferInfo *info)
  71. {
  72. (void) ctx;
  73. mpp_assert(info->ptr);
  74. mpp_assert(info->size);
  75. info->ptr = NULL;
  76. info->size = 0;
  77. info->hnd = NULL;
  78. info->fd = -1;
  79. return MPP_OK;
  80. }
  81. static MPP_RET allocator_std_mmap(void *ctx, MppBufferInfo *info)
  82. {
  83. mpp_assert(ctx);
  84. mpp_assert(info->ptr);
  85. mpp_assert(info->size);
  86. return MPP_OK;
  87. }
  88. static MPP_RET allocator_std_close(void *ctx)
  89. {
  90. MPP_FREE(ctx);
  91. return MPP_OK;
  92. }
  93. static MppAllocFlagType os_allocator_std_flags(void *ctx)
  94. {
  95. (void) ctx;
  96. return MPP_ALLOC_FLAG_NONE;
  97. }
  98. os_allocator allocator_std = {
  99. .type = MPP_BUFFER_TYPE_NORMAL,
  100. .open = allocator_std_open,
  101. .close = allocator_std_close,
  102. .alloc = allocator_std_alloc,
  103. .free = allocator_std_free,
  104. .import = allocator_std_import,
  105. .release = allocator_std_release,
  106. .mmap = allocator_std_mmap,
  107. .flags = os_allocator_std_flags,
  108. };