fd_array.hh revision 11856
111856Sbrandon.potter@amd.com/* 211856Sbrandon.potter@amd.com * Copyright (c) 2016 Advanced Micro Devices, Inc. 311856Sbrandon.potter@amd.com * All rights reserved. 411856Sbrandon.potter@amd.com * 511856Sbrandon.potter@amd.com * For use for simulation and test purposes only 611856Sbrandon.potter@amd.com * 711856Sbrandon.potter@amd.com * Redistribution and use in source and binary forms, with or without 811856Sbrandon.potter@amd.com * modification, are permitted provided that the following conditions are met: 911856Sbrandon.potter@amd.com * 1011856Sbrandon.potter@amd.com * 1. Redistributions of source code must retain the above copyright notice, 1111856Sbrandon.potter@amd.com * this list of conditions and the following disclaimer. 1211856Sbrandon.potter@amd.com * 1311856Sbrandon.potter@amd.com * 2. Redistributions in binary form must reproduce the above copyright notice, 1411856Sbrandon.potter@amd.com * this list of conditions and the following disclaimer in the documentation 1511856Sbrandon.potter@amd.com * and/or other materials provided with the distribution. 1611856Sbrandon.potter@amd.com * 1711856Sbrandon.potter@amd.com * 3. Neither the name of the copyright holder nor the names of its 1811856Sbrandon.potter@amd.com * contributors may be used to endorse or promote products derived from this 1911856Sbrandon.potter@amd.com * software without specific prior written permission. 2011856Sbrandon.potter@amd.com * 2111856Sbrandon.potter@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2211856Sbrandon.potter@amd.com * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2311856Sbrandon.potter@amd.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2411856Sbrandon.potter@amd.com * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2511856Sbrandon.potter@amd.com * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2611856Sbrandon.potter@amd.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2711856Sbrandon.potter@amd.com * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2811856Sbrandon.potter@amd.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2911856Sbrandon.potter@amd.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3011856Sbrandon.potter@amd.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3111856Sbrandon.potter@amd.com * POSSIBILITY OF SUCH DAMAGE. 3211856Sbrandon.potter@amd.com * 3311856Sbrandon.potter@amd.com * Author: Brandon Potter 3411856Sbrandon.potter@amd.com */ 3511856Sbrandon.potter@amd.com 3611856Sbrandon.potter@amd.com#ifndef __FD_ARRAY_HH__ 3711856Sbrandon.potter@amd.com#define __FD_ARRAY_HH__ 3811856Sbrandon.potter@amd.com 3911856Sbrandon.potter@amd.com#include <array> 4011856Sbrandon.potter@amd.com#include <memory> 4111856Sbrandon.potter@amd.com#include <string> 4211856Sbrandon.potter@amd.com 4311856Sbrandon.potter@amd.com#include "sim/fd_entry.hh" 4411856Sbrandon.potter@amd.com 4511856Sbrandon.potter@amd.comclass FDArray 4611856Sbrandon.potter@amd.com{ 4711856Sbrandon.potter@amd.com private: 4811856Sbrandon.potter@amd.com static const int NUM_FDS = 1024; 4911856Sbrandon.potter@amd.com 5011856Sbrandon.potter@amd.com public: 5111856Sbrandon.potter@amd.com /** 5211856Sbrandon.potter@amd.com * Initialize the file descriptor array and set the standard file 5311856Sbrandon.potter@amd.com * descriptors to defaults or values passed in with the process 5411856Sbrandon.potter@amd.com * params. 5511856Sbrandon.potter@amd.com * @param input Used to initialize the stdin file descriptor 5611856Sbrandon.potter@amd.com * @param output Used to initialize the stdout file descriptor 5711856Sbrandon.potter@amd.com * @param errout Used to initialize the stderr file descriptor 5811856Sbrandon.potter@amd.com */ 5911856Sbrandon.potter@amd.com FDArray(std::string const& input, std::string const& output, 6011856Sbrandon.potter@amd.com std::string const& errout); 6111856Sbrandon.potter@amd.com 6211856Sbrandon.potter@amd.com std::string _input; 6311856Sbrandon.potter@amd.com std::string _output; 6411856Sbrandon.potter@amd.com std::string _errout; 6511856Sbrandon.potter@amd.com 6611856Sbrandon.potter@amd.com /** 6711856Sbrandon.potter@amd.com * Figure out the file offsets for all currently open files and save them 6811856Sbrandon.potter@amd.com * the offsets during the calls to drain by the owning process. 6911856Sbrandon.potter@amd.com */ 7011856Sbrandon.potter@amd.com void updateFileOffsets(); 7111856Sbrandon.potter@amd.com 7211856Sbrandon.potter@amd.com /** 7311856Sbrandon.potter@amd.com * Restore all offsets for currently open files during the unserialize 7411856Sbrandon.potter@amd.com * phase for the owning process class. 7511856Sbrandon.potter@amd.com */ 7611856Sbrandon.potter@amd.com void restoreFileOffsets(); 7711856Sbrandon.potter@amd.com 7811856Sbrandon.potter@amd.com /** 7911856Sbrandon.potter@amd.com * Treat this object like a normal array in using the subscript operator 8011856Sbrandon.potter@amd.com * to pull entries out of it. 8111856Sbrandon.potter@amd.com * @param tgt_fd Use target file descriptors to index the array. 8211856Sbrandon.potter@amd.com */ 8311856Sbrandon.potter@amd.com inline std::shared_ptr<FDEntry> 8411856Sbrandon.potter@amd.com operator[](int tgt_fd) 8511856Sbrandon.potter@amd.com { 8611856Sbrandon.potter@amd.com return getFDEntry(tgt_fd); 8711856Sbrandon.potter@amd.com } 8811856Sbrandon.potter@amd.com 8911856Sbrandon.potter@amd.com /** 9011856Sbrandon.potter@amd.com * Step through the file descriptor array and find the first available 9111856Sbrandon.potter@amd.com * entry which is denoted as being free by being a 'nullptr'. That file 9211856Sbrandon.potter@amd.com * descriptor entry is the new target file descriptor entry that we 9311856Sbrandon.potter@amd.com * return as the return parameter. 9411856Sbrandon.potter@amd.com * @param fdp Allocated beforehand and passed into this method; 9511856Sbrandon.potter@amd.com * the fdp is meant to be a generic pointer capable of pointing to 9611856Sbrandon.potter@amd.com * different types of file descriptors. Must cast the pointer to the 9711856Sbrandon.potter@amd.com * correct type before dereferencing to access the needed fields. 9811856Sbrandon.potter@amd.com */ 9911856Sbrandon.potter@amd.com int allocFD(std::shared_ptr<FDEntry> fdp); 10011856Sbrandon.potter@amd.com 10111856Sbrandon.potter@amd.com /** 10211856Sbrandon.potter@amd.com * Return the size of the _fdArray field 10311856Sbrandon.potter@amd.com */ 10411856Sbrandon.potter@amd.com int getSize() const { return _fdArray.size(); } 10511856Sbrandon.potter@amd.com 10611856Sbrandon.potter@amd.com /** 10711856Sbrandon.potter@amd.com * Try to close the host file descriptor. If successful, set the 10811856Sbrandon.potter@amd.com * specified file descriptor entry object pointer to nullptr. 10911856Sbrandon.potter@amd.com * Used to "close" the target file descriptor. 11011856Sbrandon.potter@amd.com * @param tgt_fd Use target file descriptors to index the array. 11111856Sbrandon.potter@amd.com */ 11211856Sbrandon.potter@amd.com int closeFDEntry(int tgt_fd); 11311856Sbrandon.potter@amd.com 11411856Sbrandon.potter@amd.com private: 11511856Sbrandon.potter@amd.com /** 11611856Sbrandon.potter@amd.com * Help clarify our intention when opening files in the init and 11711856Sbrandon.potter@amd.com * restoration code. These are helper functions which are not meant to 11811856Sbrandon.potter@amd.com * be exposed to other objects or files. 11911856Sbrandon.potter@amd.com */ 12011856Sbrandon.potter@amd.com int openFile(std::string const& file_name, int flags, mode_t mode) const; 12111856Sbrandon.potter@amd.com int openInputFile(std::string const& file_name) const; 12211856Sbrandon.potter@amd.com int openOutputFile(std::string const& file_name) const; 12311856Sbrandon.potter@amd.com 12411856Sbrandon.potter@amd.com /** 12511856Sbrandon.potter@amd.com * Return the file descriptor entry object associated with the index 12611856Sbrandon.potter@amd.com * provided. (The index is protected with bounds checking on the array 12711856Sbrandon.potter@amd.com * size without the use of the array's at operator.) 12811856Sbrandon.potter@amd.com * @param tgt_fd Use target file descriptors to index the array. 12911856Sbrandon.potter@amd.com */ 13011856Sbrandon.potter@amd.com std::shared_ptr<FDEntry> getFDEntry(int tgt_fd); 13111856Sbrandon.potter@amd.com 13211856Sbrandon.potter@amd.com /** 13311856Sbrandon.potter@amd.com * Hold pointers to the file descriptor entries. The array size is 13411856Sbrandon.potter@amd.com * statically defined by the operating system. 13511856Sbrandon.potter@amd.com */ 13611856Sbrandon.potter@amd.com std::array<std::shared_ptr<FDEntry>, NUM_FDS> _fdArray; 13711856Sbrandon.potter@amd.com 13811856Sbrandon.potter@amd.com /** 13911856Sbrandon.potter@amd.com * Hold strings which represent the default values which are checked 14011856Sbrandon.potter@amd.com * against to initialize the standard file descriptors. If the string 14111856Sbrandon.potter@amd.com * provided doesn't hit against these maps, then a file is opened on the 14211856Sbrandon.potter@amd.com * host instead of using the host's standard file descriptors. 14311856Sbrandon.potter@amd.com */ 14411856Sbrandon.potter@amd.com std::map<std::string, int> imap; 14511856Sbrandon.potter@amd.com std::map<std::string, int> oemap; 14611856Sbrandon.potter@amd.com}; 14711856Sbrandon.potter@amd.com 14811856Sbrandon.potter@amd.com#endif // __FD_ARRAY_HH__ 149