|
| 1 | +/**************************************************************************** |
| 2 | + * libs/libc/misc/lib_pathbuffer.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 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | +#include <nuttx/mutex.h> |
| 27 | +#include <nuttx/lib/lib.h> |
| 28 | + |
| 29 | +#include <stdlib.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +/**************************************************************************** |
| 33 | + * Pre-processor definitions |
| 34 | + ****************************************************************************/ |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Private Types |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +struct pathbuffer_s |
| 41 | +{ |
| 42 | + mutex_t lock; /* Lock for the buffer */ |
| 43 | + int free_bitmap; /* Bitmap of free buffer */ |
| 44 | + char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX]; |
| 45 | +}; |
| 46 | + |
| 47 | +/**************************************************************************** |
| 48 | + * Private Data |
| 49 | + ****************************************************************************/ |
| 50 | + |
| 51 | +static struct pathbuffer_s g_pathbuffer = |
| 52 | +{ |
| 53 | + NXMUTEX_INITIALIZER, |
| 54 | + (1u << CONFIG_LIBC_MAX_PATHBUFFER) - 1, |
| 55 | +}; |
| 56 | + |
| 57 | +/**************************************************************************** |
| 58 | + * Private Functions |
| 59 | + ****************************************************************************/ |
| 60 | + |
| 61 | +/**************************************************************************** |
| 62 | + * Public Functions |
| 63 | + ****************************************************************************/ |
| 64 | + |
| 65 | +/**************************************************************************** |
| 66 | + * Name: lib_get_pathbuffer |
| 67 | + * |
| 68 | + * Description: |
| 69 | + * The lib_get_pathbuffer() function returns a pointer to a temporary |
| 70 | + * buffer. The buffer is allocated from a pool of pre-allocated buffers |
| 71 | + * and if the pool is exhausted, a new buffer is allocated through |
| 72 | + * kmm_malloc(). The size of the buffer is PATH_MAX, and must freed by |
| 73 | + * calling lib_put_pathbuffer(). |
| 74 | + * |
| 75 | + * Returned Value: |
| 76 | + * On success, lib_get_pathbuffer() returns a pointer to a temporary |
| 77 | + * buffer. On failure, NULL is returned. |
| 78 | + * |
| 79 | + ****************************************************************************/ |
| 80 | + |
| 81 | +FAR char *lib_get_pathbuffer(void) |
| 82 | +{ |
| 83 | + int index; |
| 84 | + |
| 85 | + /* Try to find a free buffer */ |
| 86 | + |
| 87 | + nxmutex_lock(&g_pathbuffer.lock); |
| 88 | + index = ffs(g_pathbuffer.free_bitmap) - 1; |
| 89 | + if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) |
| 90 | + { |
| 91 | + g_pathbuffer.free_bitmap &= ~(1u << index); |
| 92 | + nxmutex_unlock(&g_pathbuffer.lock); |
| 93 | + return g_pathbuffer.buffer[index]; |
| 94 | + } |
| 95 | + |
| 96 | + nxmutex_unlock(&g_pathbuffer.lock); |
| 97 | + |
| 98 | + /* If no free buffer is found, allocate a new one */ |
| 99 | + |
| 100 | + return lib_malloc(PATH_MAX); |
| 101 | +} |
| 102 | + |
| 103 | +/**************************************************************************** |
| 104 | + * Name: lib_put_pathbuffer |
| 105 | + * |
| 106 | + * Description: |
| 107 | + * The lib_put_pathbuffer() function frees a temporary buffer that was |
| 108 | + * allocated by lib_get_pathbuffer(). If the buffer was allocated |
| 109 | + * dynamically, it is freed by calling kmm_free(). Otherwise, the buffer |
| 110 | + * is marked as free in the pool of pre-allocated buffers. |
| 111 | + * |
| 112 | + * Returned Value: |
| 113 | + * None |
| 114 | + * |
| 115 | + ****************************************************************************/ |
| 116 | + |
| 117 | +void lib_put_pathbuffer(FAR char *buffer) |
| 118 | +{ |
| 119 | + int index; |
| 120 | + |
| 121 | + nxmutex_lock(&g_pathbuffer.lock); |
| 122 | + index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX; |
| 123 | + |
| 124 | + if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) |
| 125 | + { |
| 126 | + /* Mark the corresponding bit as free */ |
| 127 | + |
| 128 | + g_pathbuffer.free_bitmap |= 1u << index; |
| 129 | + nxmutex_unlock(&g_pathbuffer.lock); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + nxmutex_unlock(&g_pathbuffer.lock); |
| 134 | + |
| 135 | + /* Free the buffer if it was dynamically allocated */ |
| 136 | + |
| 137 | + lib_free(buffer); |
| 138 | +} |
0 commit comments