Blame source/uds/memoryDefs.h

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/kernelLinux/uds/memoryDefs.h#2 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#ifndef LINUX_KERNEL_MEMORY_DEFS_H
Packit Service 310c69
#define LINUX_KERNEL_MEMORY_DEFS_H 1
Packit Service 310c69
Packit Service 310c69
#include <linux/io.h>  // for PAGE_SIZE
Packit Service 310c69
Packit Service 310c69
#include "compiler.h"
Packit Service 310c69
#include "threadRegistry.h"
Packit Service 310c69
#include "typeDefs.h"
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Allocate one or more elements of the indicated type, aligning them
Packit Service 310c69
 * on the boundary that will allow them to be used in io, logging an
Packit Service 310c69
 * error if the allocation fails. The memory will be zeroed.
Packit Service 310c69
 *
Packit Service 310c69
 * @param COUNT  The number of objects to allocate
Packit Service 310c69
 * @param TYPE   The type of objects to allocate
Packit Service 310c69
 * @param WHAT   What is being allocated (for error logging)
Packit Service 310c69
 * @param PTR    A pointer to hold the allocated memory
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS or an error code
Packit Service 310c69
 **/
Packit Service 310c69
#define ALLOCATE_IO_ALIGNED(COUNT, TYPE, WHAT, PTR) \
Packit Service 310c69
  doAllocation(COUNT, sizeof(TYPE), 0, PAGE_SIZE, WHAT, PTR)
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Allocate one element of the indicated type immediately, failing if the
Packit Service 310c69
 * required memory is not immediately available.
Packit Service 310c69
 *
Packit Service 310c69
 * @param TYPE   The type of objects to allocate
Packit Service 310c69
 * @param WHAT   What is being allocated (for error logging)
Packit Service 310c69
 *
Packit Service 310c69
 * @return pointer to the memory, or NULL if the memory is not available.
Packit Service 310c69
 **/
Packit Service 310c69
#define ALLOCATE_NOWAIT(TYPE, WHAT) allocateMemoryNowait(sizeof(TYPE), WHAT)
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Perform termination of the memory allocation subsystem.
Packit Service 310c69
 **/
Packit Service 310c69
void memoryExit(void);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Perform initialization of the memory allocation subsystem.
Packit Service 310c69
 **/
Packit Service 310c69
void memoryInit(void);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Allocate storage based on memory size, failing immediately if the required
Packit Service 310c69
 * memory is not available.  The memory will be zeroed.
Packit Service 310c69
 *
Packit Service 310c69
 * @param size  The size of an object.
Packit Service 310c69
 * @param what  What is being allocated (for error logging)
Packit Service 310c69
 *
Packit Service 310c69
 * @return pointer to the allocated memory, or NULL if the required space is
Packit Service 310c69
 *         not available.
Packit Service 310c69
 **/
Packit Service 310c69
void *allocateMemoryNowait(size_t size, const char *what)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Register the current thread as an allocating thread.
Packit Service 310c69
 *
Packit Service 310c69
 * An optional flag location can be supplied indicating whether, at
Packit Service 310c69
 * any given point in time, the threads associated with that flag
Packit Service 310c69
 * should be allocating storage.  If the flag is false, a message will
Packit Service 310c69
 * be logged.
Packit Service 310c69
 *
Packit Service 310c69
 * If no flag is supplied, the thread is always allowed to allocate
Packit Service 310c69
 * storage without complaint.
Packit Service 310c69
 *
Packit Service 310c69
 * @param newThread  RegisteredThread structure to use for the current thread
Packit Service 310c69
 * @param flagPtr    Location of the allocation-allowed flag
Packit Service 310c69
 **/
Packit Service 310c69
void registerAllocatingThread(RegisteredThread *newThread,
Packit Service 310c69
                              const bool       *flagPtr);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Unregister the current thread as an allocating thread.
Packit Service 310c69
 **/
Packit Service 310c69
void unregisterAllocatingThread(void);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Get the memory statistics.
Packit Service 310c69
 *
Packit Service 310c69
 * @param bytesUsed     A pointer to hold the number of bytes in use
Packit Service 310c69
 * @param peakBytesUsed A pointer to hold the maximum value bytesUsed has
Packit Service 310c69
 *                      attained
Packit Service 310c69
 **/
Packit Service 310c69
void getMemoryStats(uint64_t *bytesUsed, uint64_t *peakBytesUsed);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Report stats on any allocated memory that we're tracking.
Packit Service 310c69
 *
Packit Service 310c69
 * Not all allocation types are guaranteed to be tracked in bytes
Packit Service 310c69
 * (e.g., bios).
Packit Service 310c69
 **/
Packit Service 310c69
void reportMemoryUsage(void);
Packit Service 310c69
Packit Service 310c69
Packit Service 310c69
#endif /* LINUX_KERNEL_MEMORY_DEFS_H */