You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2021/06/17 11:22:06 UTC

[mynewt-core] 01/06: hal: Add common _sbrk function

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit fee9bdc49621b46e9f419cd8b2b89417e7cfacfc
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Wed Jun 16 09:59:03 2021 +0200

    hal: Add common _sbrk function
    
    _sbrk implementations are put in every bsp.
    Almost all implementations are identical with
    some slight variations.
    This adds implementation that will fit most
    and bsps' versions can be removed if needed.
---
 hw/hal/src/sbrk.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/hal/syscfg.yml |  6 +++++-
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/hw/hal/src/sbrk.c b/hw/hal/src/sbrk.c
new file mode 100644
index 0000000..1c11031
--- /dev/null
+++ b/hw/hal/src/sbrk.c
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+#include <syscfg/syscfg.h>
+
+#if MYNEWT_VAL(HAL_SBRK)
+
+static char *sbrk_base;
+static char *sbrk_limit;
+static char *brk;
+
+void
+_sbrkInit(char *base, char *limit)
+{
+    sbrk_base = base;
+    sbrk_limit = limit;
+    brk = base;
+}
+
+void *
+_sbrk(int incr)
+{
+    char *prev_brk;
+    char *new_brk = brk + incr;
+
+    if (new_brk < sbrk_base || new_brk > sbrk_limit) {
+        prev_brk = (char *)-1;
+    } else {
+        prev_brk = brk;
+        brk = new_brk;
+    }
+
+    return prev_brk;
+}
+
+#endif
diff --git a/hw/hal/syscfg.yml b/hw/hal/syscfg.yml
index 29bd002..381609f 100644
--- a/hw/hal/syscfg.yml
+++ b/hw/hal/syscfg.yml
@@ -42,7 +42,11 @@ syscfg.defs:
             If set to 0 software breakpoints placed with HAL_DEBUG_BREAK macro will not
             be executed.
         value: 1
-
+    HAL_SBRK:
+        description: >
+            If set HAL provides standard implementation of _sbrk function.
+            It also provides _sbrkInit function that sets up heap space for malloc.
+        value: 1
 syscfg.vals.OS_DEBUG_MODE:
     HAL_FLASH_VERIFY_WRITES: 1
     HAL_FLASH_VERIFY_ERASES: 1