modlib_iobuffer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /****************************************************************************
  2. * libs/libc/modlib/modlib_iobuffer.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <debug.h>
  25. #include <errno.h>
  26. #include <nuttx/lib/modlib.h>
  27. #include "libc.h"
  28. #include "modlib/modlib.h"
  29. /****************************************************************************
  30. * Public Functions
  31. ****************************************************************************/
  32. /****************************************************************************
  33. * Name: modlib_allocbuffer
  34. *
  35. * Description:
  36. * Perform the initial allocation of the I/O buffer, if it has not already
  37. * been allocated.
  38. *
  39. * Returned Value:
  40. * 0 (OK) is returned on success and a negated errno is returned on
  41. * failure.
  42. *
  43. ****************************************************************************/
  44. int modlib_allocbuffer(FAR struct mod_loadinfo_s *loadinfo)
  45. {
  46. /* Has a buffer been allocated> */
  47. if (!loadinfo->iobuffer)
  48. {
  49. /* No.. allocate one now */
  50. loadinfo->iobuffer = (FAR uint8_t *)
  51. lib_malloc(CONFIG_MODLIB_BUFFERSIZE);
  52. if (!loadinfo->iobuffer)
  53. {
  54. berr("ERROR: Failed to allocate an I/O buffer\n");
  55. return -ENOMEM;
  56. }
  57. loadinfo->buflen = CONFIG_MODLIB_BUFFERSIZE;
  58. }
  59. return OK;
  60. }
  61. /****************************************************************************
  62. * Name: modlib_reallocbuffer
  63. *
  64. * Description:
  65. * Increase the size of I/O buffer by the specified buffer increment.
  66. *
  67. * Returned Value:
  68. * 0 (OK) is returned on success and a negated errno is returned on
  69. * failure.
  70. *
  71. ****************************************************************************/
  72. int modlib_reallocbuffer(FAR struct mod_loadinfo_s *loadinfo,
  73. size_t increment)
  74. {
  75. FAR void *buffer;
  76. size_t newsize;
  77. /* Get the new size of the allocation */
  78. newsize = loadinfo->buflen + increment;
  79. /* And perform the reallocation */
  80. buffer = lib_realloc((FAR void *)loadinfo->iobuffer, newsize);
  81. if (!buffer)
  82. {
  83. berr("ERROR: Failed to reallocate the I/O buffer\n");
  84. return -ENOMEM;
  85. }
  86. /* Save the new buffer info */
  87. loadinfo->iobuffer = buffer;
  88. loadinfo->buflen = newsize;
  89. return OK;
  90. }