casaroli commented on code in PR #19536:
URL: https://github.com/apache/nuttx/pull/19536#discussion_r3673204823


##########
fs/xipfs/xipfs_alloc.c:
##########
@@ -0,0 +1,285 @@
+/****************************************************************************
+ * fs/xipfs/xipfs_alloc.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ *
+ * Allocation is in whole erase blocks and always contiguous.  Because a
+ * file's full size is known when it is created, the exact extent is
+ * reserved up front and never grows, so a file can never become internally
+ * fragmented.  The only source of fragmentation is the holes left behind by
+ * deletes, which is what the defragmenter coalesces.
+ *
+ * The free map is a bitmap over the data region, derived entirely from the
+ * committed directory: it is state that can always be rebuilt, never state
+ * that has to be recovered.
+ *
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/kmalloc.h>
+
+#include "xipfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static inline bool xipfs_bitmap_isset(FAR struct xipfs_mount_s *fs,
+                                      uint32_t index)
+{
+  return (fs->bitmap[index >> 3] & (1 << (index & 7))) != 0;
+}
+
+static inline void xipfs_bitmap_set(FAR struct xipfs_mount_s *fs,
+                                    uint32_t index)
+{
+  fs->bitmap[index >> 3] |= (1 << (index & 7));
+}
+
+static inline void xipfs_bitmap_clr(FAR struct xipfs_mount_s *fs,
+                                    uint32_t index)
+{
+  fs->bitmap[index >> 3] &= ~(1 << (index & 7));
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: xipfs_alloc_init
+ *
+ * Description:
+ *   Allocate the free bitmap.  Sized for the data region only.
+ *
+ ****************************************************************************/
+
+int xipfs_alloc_init(FAR struct xipfs_mount_s *fs)
+{
+  fs->bitmapsize = (fs->data_nblocks + 7) >> 3;
+  fs->bitmap     = kmm_zalloc(fs->bitmapsize);
+
+  if (fs->bitmap == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: xipfs_alloc_rebuild
+ *
+ * Description:
+ *   Recompute the free bitmap from the extent list.  Called after mount and
+ *   after any change that moves extents.
+ *
+ ****************************************************************************/
+
+void xipfs_alloc_rebuild(FAR struct xipfs_mount_s *fs)
+{
+  FAR struct xipfs_extent_s *ext;
+  uint32_t i;
+
+  memset(fs->bitmap, 0, fs->bitmapsize);
+
+  for (ext = fs->extents; ext != NULL; ext = ext->flink)
+    {
+      /* A directory record owns no blocks */
+
+      for (i = 0; i < ext->nblocks; i++)
+        {
+          uint32_t index = ext->start_block - fs->data_start + i;
+
+          DEBUGASSERT(index < fs->data_nblocks);
+          xipfs_bitmap_set(fs, index);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: xipfs_alloc
+ *
+ * Description:
+ *   Reserve a contiguous run of 'nblocks' erase blocks using best fit.
+ *
+ *   This deliberately does NOT invoke the defragmenter on failure.  A
+ *   caller that hits -ENOSPC gets to decide whether compacting is worth it
+ *   right now, and the defragmenter therefore always runs from a known
+ *   clean state rather than from inside a half-finished allocation.
+ *
+ * Returned Value:
+ *   OK with *start_block set, or -ENOSPC if no single contiguous run of
+ *   the requested size exists.
+ *
+ ****************************************************************************/
+
+int xipfs_alloc(FAR struct xipfs_mount_s *fs, uint32_t nblocks,

Review Comment:
   done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to