Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RIOT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cm-projects
RIOT
Commits
c85d33f3
Commit
c85d33f3
authored
6 years ago
by
Kaspar Schleiser
Browse files
Options
Downloads
Patches
Plain Diff
sys/include/unaligned: initial commit
parent
d34bf526
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
sys/include/unaligned.h
+69
-0
69 additions, 0 deletions
sys/include/unaligned.h
with
69 additions
and
0 deletions
sys/include/unaligned.h
0 → 100644
+
69
−
0
View file @
c85d33f3
/*
* Copyright (C) 2019 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup sys_unaligned unaligned memory access methods
* @ingroup sys
* @brief Provides functions for safe unaligned memory accesses
*
* This header provides functions to read values from pointers that are not
* necessarily aligned to the type's alignment requirements.
*
* E.g.,
*
* uint16_t *foo = 0x123;
* printf("%u\n", *foo);
*
* ... might cause an unaligned access, if `uint16_t` is usually aligned at
* 2-byte-boundaries, as foo has an odd address.
*
* The current implementation casts a pointer to a packed struct, which forces
* the compiler to deal with possibly unalignedness. Idea taken from linux
* kernel sources.
*
* @{
*
* @file
* @brief Unaligned but safe memory access functions
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*/
#ifndef UNALIGNED_H
#define UNALIGNED_H
#include
<stdint.h>
#ifdef __cplusplus
extern
"C"
{
#endif
/** @brief Unaligned access helper struct (uint16_t version) */
typedef
struct
__attribute__
((
packed
))
{
uint16_t
val
;
/**< value */
}
uint16_una_t
;
/**
* @brief Get uint16_t from possibly unaligned pointer
*
* @param[in] ptr pointer to read from
*
* @returns value read from @p ptr
*/
static
inline
uint16_t
unaligned_get_u16
(
const
void
*
ptr
)
{
const
uint16_una_t
*
tmp
=
ptr
;
return
tmp
->
val
;
}
#ifdef __cplusplus
}
#endif
/** @} */
#endif
/* UNALIGNED_H */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment