forked from ros2/rcutils
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* micro-ROS changes over dashing Feature/add security directory (#1) * Added security directory * Updated security directory Feature/avoid filesystem and allocation (#2) * Included RCUTILS_NO_FILESYSTEM and RCUTILS_AVOID_DYNAMIC_ALLOCATION * Added no filesystem options * Default allocators write access * Avoid dynamic allocation and no filesytem on error handling * Typo * New flags for filesystem and avoid dynamic * Error handling template * New allocator approach Add test_security_directory test from rcl. (#3) Merge pull request #4 from micro-ROS/feature/zephyr_fixes Feature/zephyr fixes CMake refactor (#5) Update approach (#6) * Update approach * Remove target_compile_definitions and refactor flags install * Added RCUTILS_NO_FILESYSTEM on new functions * Added RCUTILS_NO_FILESYSTEM on new functions Co-authored-by: Pablo Garrido <pablogs9@gmail.com> * Initial changes * Add hashing and lock pool * Updates Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com> Fix atomics 64bits (#9)
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed 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. | ||
|
||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
|
||
#define FLAGS_LEN 23 | ||
|
||
static bool * get_memory_lock(void *address) | ||
{ | ||
static bool memory_locks[FLAGS_LEN] = { 0 }; | ||
uintptr_t a = (uintptr_t)(address); | ||
|
||
// Public domain hash function taken from http://burtleburtle.net/bob/hash/integer.html | ||
a = (a ^ 61) ^ (a >> 16); | ||
a = a + (a << 3); | ||
a = a ^ (a >> 4); | ||
a = a * 0x27d4eb2d; | ||
a = a ^ (a >> 15); | ||
|
||
a = a % FLAGS_LEN; | ||
return memory_locks + a; | ||
} | ||
|
||
void lock_memory(uint64_t *address){ | ||
bool * memory_lock = get_memory_lock(address); | ||
|
||
while (__atomic_test_and_set(memory_lock, __ATOMIC_ACQUIRE) == 1); | ||
} | ||
|
||
void unlock_memory(uint64_t *address){ | ||
bool * memory_lock = get_memory_lock(address); | ||
|
||
__atomic_clear(memory_lock, __ATOMIC_RELEASE); | ||
} | ||
|
||
uint64_t __atomic_load_8(uint64_t *mem, int model) { | ||
(void) model; | ||
|
||
lock_memory(mem); | ||
uint64_t ret = *mem; | ||
unlock_memory(mem); | ||
return ret; | ||
} | ||
|
||
void __atomic_store_8(uint64_t *mem, uint64_t val, int model) { | ||
(void) model; | ||
|
||
lock_memory(mem); | ||
*mem = val; | ||
unlock_memory(mem); | ||
} | ||
|
||
uint64_t __atomic_exchange_8(uint64_t *mem, uint64_t val, int model) { | ||
(void) model; | ||
|
||
lock_memory(mem); | ||
uint64_t ret = *mem; | ||
*mem = val; | ||
unlock_memory(mem); | ||
return ret; | ||
} | ||
|
||
uint64_t __atomic_fetch_add_8(uint64_t *mem, uint64_t val, int model) { | ||
(void) model; | ||
|
||
lock_memory(mem); | ||
uint64_t ret = *mem; | ||
*mem += val; | ||
unlock_memory(mem); | ||
return ret; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |