blob: 78ba3de62cc7c72aceef19ff4473ec4819e4121f [file] [log] [blame]
Anders Lewisf4447b92017-06-23 15:53:59 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "util.h"
18
Anders Lewisa98a5fb2017-08-09 16:52:19 -070019#include <err.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070020#include <math.h>
Anders Lewisf4447b92017-06-23 15:53:59 -070021#include <sched.h>
22#include <stdio.h>
23#include <string.h>
Elliott Hughese86a03f2025-06-05 09:23:19 -070024#include <sys/param.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070025#include <wchar.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070026
Anders Lewisf4447b92017-06-23 15:53:59 -070027#include <cstdlib>
Anders Lewisf4447b92017-06-23 15:53:59 -070028
29// This function returns a pointer less than 2 * alignment + or_mask bytes into the array.
Anders Lewisa7b0f882017-07-24 20:01:13 -070030char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask) {
Elliott Hughese86a03f2025-06-05 09:23:19 -070031 if (!powerof2(alignment)) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -070032 errx(1, "warning: alignment passed into GetAlignedMemory is not a power of two.");
Anders Lewisf4447b92017-06-23 15:53:59 -070033 }
34 if (or_mask > alignment) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -070035 errx(1, "warning: or_mask passed into GetAlignedMemory is too high.");
Anders Lewisf4447b92017-06-23 15:53:59 -070036 }
37 uintptr_t ptr = reinterpret_cast<uintptr_t>(orig_ptr);
38 if (alignment > 0) {
39 // When setting the alignment, set it to exactly the alignment chosen.
40 // The pointer returned will be guaranteed not to be aligned to anything
41 // more than that.
42 ptr += alignment - (ptr & (alignment - 1));
43 ptr |= alignment | or_mask;
44 }
45
46 return reinterpret_cast<char*>(ptr);
47}
48
Anders Lewisa7b0f882017-07-24 20:01:13 -070049char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes) {
Anders Lewisf4447b92017-06-23 15:53:59 -070050 buf->resize(nbytes + 3 * alignment);
51 return GetAlignedMemory(buf->data(), alignment, 0);
52}
53
Anders Lewisac4f4b42017-08-08 18:29:51 -070054wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nchars) {
55 buf->resize(nchars + ceil((3 * alignment) / sizeof(wchar_t)));
56 return reinterpret_cast<wchar_t*>(GetAlignedMemory(reinterpret_cast<char*>(buf->data()),
57 alignment, 0));
58}
59
Anders Lewisa7b0f882017-07-24 20:01:13 -070060char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte) {
Anders Lewisf4447b92017-06-23 15:53:59 -070061 char* buf_aligned = GetAlignedPtr(buf, alignment, nbytes);
62 memset(buf_aligned, fill_byte, nbytes);
63 return buf_aligned;
64}
65
Christopher Ferris5fc1f6e2017-07-12 23:10:07 -070066#if defined(__APPLE__)
Christopher Ferrisbad43c72017-07-12 19:33:32 -070067
68// Darwin doesn't support this, so do nothing.
69bool LockToCPU(int) {
70 return false;
71}
72
73#else
74
Christopher Ferrisbfb7c762018-05-04 13:27:47 -070075bool LockToCPU(int cpu_to_lock) {
Anders Lewisf4447b92017-06-23 15:53:59 -070076 cpu_set_t cpuset;
77
78 CPU_ZERO(&cpuset);
Anders Lewisf4447b92017-06-23 15:53:59 -070079 CPU_SET(cpu_to_lock, &cpuset);
80 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
Christopher Ferrisbfb7c762018-05-04 13:27:47 -070081 if (errno == EINVAL) {
82 printf("Invalid cpu %d\n", cpu_to_lock);
83 } else {
84 perror("sched_setaffinity failed");
85 }
Anders Lewisf4447b92017-06-23 15:53:59 -070086 return false;
87 }
88
89 return true;
90}
Christopher Ferrisbad43c72017-07-12 19:33:32 -070091
92#endif