"Fossies" - the Fresh Open Source Software Archive

Member "bed-3.1.1/src/memory.h" (20 May 2021, 8434 Bytes) of package /linux/misc/bed-3.1.1.src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "memory.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_3.1.1.

    1 /*     Bed a Binary EDitor for Linux and for Rxvt running under Linux.       */
    2 /*     Copyright (C) 1998  Jaap Korthals Altes <binaryeditor@gmx.com>      */
    3 /*                                                                           */
    4 /*     Bed is free software; you can redistribute it and/or modify           */
    5 /*     it under the terms of the GNU General Public License as published by  */
    6 /*     the Free Software Foundation; either version 2 of the License, or     */
    7 /*     (at your option) any later version.                                   */
    8 /*                                                                           */
    9 /*     Bed is distributed in the hope that it will be useful,                */
   10 /*     but WITHOUT ANY WARRANTY; without even the implied warranty of        */
   11 /*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
   12 /*     GNU General Public License for more details.                          */
   13 /*                                                                           */
   14 /*     You should have received a copy of the GNU General Public License     */
   15 /*     along with bed; if not, write to the Free Software                    */
   16 /*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
   17 /*                                                                           */
   18 /* Sun Dec  6 18:34:27 1998                                                  */
   19 
   20 #ifndef MEMORY_H
   21 #define MEMORY_H
   22 #include <features.h>
   23 #include <limits.h>
   24 #include <sys/types.h>
   25 #include <sys/param.h>
   26 #include <string.h>
   27 #include <stdlib.h>
   28 
   29 #ifdef USE_RE2
   30 #include <re2/re2.h>
   31 #endif
   32 #include "defines.h"
   33 #include "rbtree.h"
   34 #include "offtypes.h"
   35 #include "myalloc.h"
   36 #if defined(__FreeBSD__)||defined( _WIN32 )
   37 #define NEEDS_BLOCKMODE 1
   38 #ifdef _WIN32
   39 #define ALIGNMEMORY 1
   40 #endif
   41 #endif
   42 #include "alignmem.h"
   43 #ifdef  __cplusplus
   44 extern "C" {
   45 #endif
   46 #ifdef HAS_RE_SEARCH
   47 #ifdef HAVE_GNUREGEX_H
   48 #include <gnuregex.h>
   49 #else
   50 #include <regex.h>
   51 #endif
   52 #else
   53 #include "../regex-0.13/xregex.h"
   54 #endif
   55 
   56 
   57 #ifdef  __cplusplus
   58 }
   59 #endif
   60 #define DEBUG DEBUGGING
   61 #include "debug.h"
   62 #define BIGEST   INT_MAX
   63 
   64 #if !defined(USE_PREAD) && defined(USETHREADS)
   65 #include "nepsem.h"
   66 
   67 #ifdef HAS_SEMAPHORE 
   68 #include <semaphore.h>
   69 #define DESTROYUSELSEEK  sem_destroy(&lseeksem)
   70 #else
   71 #define DESTROYUSELSEEK  
   72 #endif
   73 #define DEFINEUSELSEEK sem_t lseeksem
   74 #define INITUSELSEEK sem_init(&lseeksem,0,1)
   75 #define STARTUSELSEEK sem_wait(&lseeksem)
   76 #define STOPUSELSEEK sem_post(&lseeksem)
   77 #define GSTOPUSELSEEK(memp) sem_post(&((memp)->lseeksem))
   78 #else
   79 #define GSTOPUSELSEEK(memp)
   80 #define DEFINEUSELSEEK 
   81 #define DESTROYUSELSEEK  
   82 #define INITUSELSEEK 
   83 #define STARTUSELSEEK
   84 #define STOPUSELSEEK 
   85 #endif
   86 
   87 extern int STBLOCKSIZE;
   88 
   89 
   90 //#define DEBUG(...) fprintf(stderr,__VA_ARGS__)
   91 //#define DEBUG(x...)
   92 #undef minnum
   93 #undef maxnum
   94 #define minnum( x,y) ((x)>(y)?(y):(x))
   95 #define maxnum( een, twee) (((een)>(twee))?(een):(twee))
   96 typedef char Flag;
   97 #ifdef BIOSDISK
   98 #include "biosdisk.h"
   99 #endif
  100 #ifndef _WIN32
  101 typedef int BOOL ;
  102 #endif
  103 
  104 class Editor;
  105 class Memory {
  106 #ifdef USE_WINFILE
  107 bool    used_dasd_io;
  108 #endif
  109 public:
  110 #ifdef BIOSDISK
  111     Diskinfo *infodisk; 
  112 #endif
  113 private:
  114 #ifdef NEEDS_BLOCKMODE
  115     int blockreadonly;
  116 #endif
  117 
  118         public:
  119 
  120 DEFINEUSELSEEK ;
  121 int BLOCKSIZE;
  122     FILEHANDLE filehandle;
  123     int errors,readonly;
  124 Treel *gettreel(BLOCKTYPE blockiter) ;
  125 
  126 #ifdef _WIN32
  127     BY_HANDLE_FILE_INFORMATION FileInfo;
  128 
  129 int getfileinfo( BY_HANDLE_FILE_INFORMATION *FInfo ) ;
  130 #else
  131      STATTYPE statdata;
  132 
  133 bool statmis() ;
  134 #endif
  135 
  136     OFFTYPE filesize, diskfilesize{},useddiskfilesize{};
  137 
  138 
  139     char *filename;
  140     int filenamelen;
  141     Tree tree;
  142     int error(void) const {return errors;}
  143     inline bool isnewfile() { return filehandle== MY_INVALID_HANDLE_VALUE;};
  144     OFFTYPE size(void) const {return filesize;}
  145 explicit    Memory(const char *name=NULL);
  146 
  147     Memory() = delete;
  148     Memory(Memory const &) = delete;
  149     Memory& operator=(Memory const&) = delete;
  150     ~Memory();
  151 
  152 
  153 //void setsearchstr(const signed char *const str,const int len) ;
  154 void openfile(void) ;
  155 void init(const char *name) ;
  156 void initall(void) ;
  157 //int reinit(const char *name) ;
  158 //int reinit(void) ;
  159 
  160 void endfile(void);
  161 int empty(void) ;
  162 
  163 int savenode(FILEHANDLE output, Treel *node) ;
  164     int saveall(void);
  165 #if (defined(HAVE_SENDFILE) && defined(SEEK_DATA)) ||defined(USE_WINFILE)
  166     int nonsparsesaveto(FILEHANDLE handle);
  167 
  168 #endif
  169     int saveto(FILEHANDLE handle);
  170     BEDS_SIZE_T saveto(FILEHANDLE handle,OFFTYPE beg,OFFTYPE len) ;
  171 
  172 #if (defined(HAVE_SENDFILE) && defined(SEEK_DATA)) ||defined(USE_WINFILE)
  173 BEDS_SIZE_T nonsparsesaveto(FILEHANDLE handle,OFFTYPE beg,OFFTYPE len) ;
  174 #endif
  175     int touched(void) ;
  176     BEDS_SIZE_T getblock(BLOCKTYPE blocknr,char *buf) ;
  177 OFFTYPE  getblocks(const BLOCKTYPE een,char *buf,const BLOCKTYPE nr) ;
  178 OFFTYPE  putblocks(const BLOCKTYPE een,char *buf,const BLOCKTYPE nr) ;
  179 
  180     Flag mmapfailed=1;
  181 BEDS_SIZE_T mmapgetblock(BLOCKTYPE blocknr,char *buf) ;
  182     BEDS_SIZE_T putblock(BLOCKTYPE blocknr,char *buf) ;
  183     BEDS_SIZE_T getblockpiece(OFFTYPE beg,BEDS_SIZE_T len,char *get) ;
  184 
  185 #ifdef _WIN32
  186 OFFTYPE getpartaligned(const OFFTYPE beg,const OFFTYPE len,char *const gegs) ;
  187 #endif
  188     OFFTYPE getpart(OFFTYPE beg,OFFTYPE len,char *gegs) ;
  189 BEDS_SIZE_T putblockpiece(OFFTYPE  beg,BEDS_SIZE_T len,char *get) ;
  190 OFFTYPE putpart(OFFTYPE beg,OFFTYPE len,char *gegs) ;
  191 void    setborder(OFFTYPE pos) ;
  192 void extend(OFFTYPE newsize) ;
  193 void undotruncate(OFFTYPE newsize,OFFTYPE useddisk) ;
  194 void truncatefile(OFFTYPE pos) ;            /* v2 */
  195 //f c r
  196 
  197 inline BLOCKTYPE blocksize(void) const { return BLOCKSIZE;};
  198 
  199 BEDS_SIZE_T realblockreadat(char *ptr,BEDS_SIZE_T size,OFFTYPE offset) ;
  200 BEDS_SIZE_T blockreadat(char *ptr,BEDS_SIZE_T size,OFFTYPE offset) ;
  201 inline  BEDS_SIZE_T readat(char *ptr,BEDS_SIZE_T size,OFFTYPE offset) ;
  202 
  203 BEDS_SIZE_T writeat(FILEHANDLE output,char *ptr,BEDS_SIZE_T size,OFFTYPE offset) ;
  204 
  205 int zerochange(void) ;
  206 int changed(void) ;
  207 int changednode(Treel *node) ;
  208 void exploresize(FILEHANDLE file,OFFTYPE begin=0, OFFTYPE end=MAXDISCSIZE) ;
  209 
  210 
  211 #ifdef BIOSDISK
  212 OFFTYPE getregistrysize(Diskinfo *infodisk,OFFTYPE &begin,OFFTYPE &end) ;
  213 BOOL setregistrysize(Diskinfo *infodisk,OFFTYPE filesize);
  214 inline BOOL isbiosdisk() {return infodisk!=NULL;};
  215 
  216 inline bool opendosdrive(const char *const filename,OFFTYPE &begin, OFFTYPE &end) ;
  217 #endif
  218 inline BOOL sizehigher(OFFTYPE sizefile) ;
  219 inline BOOL testsize(OFFTYPE sizetest);
  220 inline BOOL checkfilesize(OFFTYPE filsize,OFFTYPE &begin,OFFTYPE &end) ;
  221 OFFTYPE win32diskfilesize(FILEHANDLE filehandle,OFFTYPE *,OFFTYPE *) ;
  222 #ifdef __linux__
  223 bool getsizelinux(OFFTYPE &begin,OFFTYPE &end) ;
  224 #endif
  225 
  226 inline void regfilesize(void ) ;
  227 
  228 #ifdef USE_WINFILE
  229 inline FILEHANDLE openreading(const char *filename) ;
  230 inline FILEHANDLE openreadwrite(const char *filename) ;
  231 
  232 #endif
  233 
  234 FILEHANDLE reopen() ;
  235     };
  236 
  237 
  238 extern BEDS_SIZE_T writetofile(FILEHANDLE output,const char *buf,BEDS_SIZE_T nr) ;
  239 
  240 #ifdef _WIN32
  241 inline ssize_t sgetfilesize(FILEHANDLE han) {
  242         return GetFileSize(han,NULL);
  243         }
  244 inline OFFTYPE lgetfilesize(FILEHANDLE han) {
  245         DWORD high,low;
  246         low=GetFileSize(han,&high);
  247         return (OFFTYPE)high<<32|(OFFTYPE)low;
  248         }
  249 /*
  250 inline OFFTYPE lgetfilesize(FILEHANDLE han) {
  251 LARGE_INTEGER siz;
  252 GetFileSizeEx(han,&siz);
  253 return siz.QuadPart;
  254 }
  255 */
  256 
  257 /*
  258 
  259 LARGE_INTEGER siz;
  260 GetFileSizeEx(han,&siz);
  261 return siz.QuadPart;
  262 BOOL WINAPI GetFileSizeEx(
  263   __in   HANDLE hFile,
  264   __out  PLARGE_INTEGER lpFileSize
  265 );
  266 ypedef union _LARGE_INTEGER {
  267   struct {
  268     DWORD LowPart;
  269     LONG  HighPart;
  270   } ;
  271   struct {
  272     DWORD LowPart;
  273     LONG  HighPart;
  274   } u;
  275   LONGLONG QuadPart;
  276 } LARGE_INTEGER, *PLARGE_INTEGER;
  277 
  278 */
  279 #else
  280 
  281 
  282 inline ssize_t sgetfilesize(FILEHANDLE handle) {
  283         ssize_t ret=         lseek(handle,0L,SEEK_END);
  284         lseek(handle,0L,SEEK_SET);
  285     return ret;
  286         }
  287 inline OFFTYPE lgetfilesize(FILEHANDLE handle) {
  288         OFFTYPE ret=          LSEEK(handle,(OFFTYPE)0L,SEEK_END);
  289          LSEEK(handle,(OFFTYPE)0L,SEEK_SET);
  290     return ret;
  291         }
  292 #endif
  293 inline int closehandle(FILEHANDLE han) {
  294 #if defined(Windows)&&defined(USE_WINFILE) 
  295 return CloseHandle(han);
  296 #else
  297 return (LCLOSE(han)>=0);
  298 #endif
  299 }
  300 
  301 OFFTYPE setfilepos(FILEHANDLE file,OFFTYPE offset) ;
  302 #define INTSEARCH
  303 class sectorbuf {
  304     private:
  305     char *buf;
  306     public:
  307 inline  sectorbuf(int bs,size_t len) {
  308         buf=alignedalloc(bs,len);
  309         }
  310 inline  ~sectorbuf() {
  311         alignedfree(buf);
  312         }
  313 inline  char *data(void) {return buf;}
  314 };
  315 #endif