Skip to content

Commit 152894e

Browse files
committed
[libc] implement memalignment
1 parent fe6bced commit 152894e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

libc/src/stdlib/memalignment.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "src/stdlib/memalignment.h"
2+
#include "src/__support/macros/config.h"
3+
4+
namespace LIBC_NAMESPACE_DECL {
5+
6+
LLVM_LIBC_FUNCTION(size_t, memalignment, (const void *p)) {
7+
if (p == NULL) {
8+
return 0;
9+
}
10+
11+
uintptr_t addr = (uintptr_t)p;
12+
13+
// Find the rightmost set bit, which represents the maximum alignment
14+
// The alignment is a power of two, so we need to find the largest
15+
// power of two that divides the address
16+
return addr & (~addr + 1);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/memalignment.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for memalignment --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDLIB_MEM_ALIGNMENT_H
10+
#define LLVM_LIBC_SRC_STDLIB_MEM_ALIGNMENT_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
size_t memalignment(const void* p);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_STDLIB_LDIV_H
22+

0 commit comments

Comments
 (0)