memorycache.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 1,455 行 · 第 1/3 页
JAVA
1,455 行
int new_index = getDeviceIndex(page); if (new_index != lastDevice) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.print("(" + new_index + ")"); lastDevice = new_index; owd[lastDevice].doSpeed(); } // write the page pmb.writePagePacket(local_page, cache[page], 1, len[page]); // clear pageState flag pageState [page] = READ_CRC; lastPageRead = NONE; writeLog [log] = EMPTY; } } } } while (jobs); // write the bitmap of used pages for OTP device if (canRedirect) { // make a buffer that contains only then new '0' bits in the bitmap // required to not overprogram any bits int numBytes = totalPages / 8; if (numBytes == 0) numBytes = 1; boolean changed = false; byte[] temp_buf = new byte[numBytes]; for (int i = 0; i < numBytes; i++) { temp_buf[i] = (byte)(~(pbmCache[i] ^ pbmCacheModified[i]) & 0x00FF); if ((byte)temp_buf[i] != (byte)0xFF) changed = true; } //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) { System.out.print("_device bitmap: " ); debugDump(pbmCache, 0, pbmCache.length); System.out.print("_modified bitmap: " ); debugDump(pbmCacheModified, 0, pbmCacheModified.length); System.out.print("_page bitmap to write, changed: " + changed + " "); debugDump(temp_buf, 0, temp_buf.length); } // write if changed if (changed) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_writing page bitmap"); // turn off read-back verification pbmBank.setWriteVerification(false); // write buffer pbmBank.write(0, temp_buf, 0, numBytes); // readback to make sure that it matches pbmCacheModified pbmBank.read(0, false, temp_buf, 0, numBytes); for (int i = 0; i < numBytes; i++) { if ((temp_buf[i] & 0x00FF) != (pbmCacheModified[i] & 0x00FF)) throw new OneWireException("Readback verfication of page bitmap was not correct"); } // put new value of bitmap pbmCache System.arraycopy(temp_buf, 0, pbmCache, 0, numBytes); System.arraycopy(temp_buf, 0, pbmCacheModified, 0, numBytes); } } } //-------- //-------- Owner tracking methods //-------- /** * Add an owner to this memory cache. This will be tracked * for later cleanup. * * @param tobj owner of instance */ public void addOwner(Object tobj) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___addOwner"); if (owners.indexOf(tobj) == -1) { owners.addElement(tobj); } } /** * Remove the specified owner of this memory cache. * * @param tobj owner of instance */ public void removeOwner(Object tobj) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___removeOwner"); owners.removeElement(tobj); } /** * Check to see if there on no owners of this memory cache. * * @return true if not owners of this memory cache */ public boolean noOwners() { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___noOwners = " + owners.isEmpty()); return owners.isEmpty(); } //-------- //-------- Write file tracking methods //-------- /** * Remove the provided filePath from the list of files * currently opened to write. * * @param filePath file to remove from write list */ public void removeWriteOpen(String filePath) { int index = openedToWrite.indexOf(filePath); if (index != -1) openedToWrite.removeElementAt(index); } /** * Check to see if the provided filePath is currently opened * to write. Optionally add it to the list if it not already * there. * * @param filePath file to check to see if opened to write * @param addToList true to add file to list if not present * * @return true if file was not in the opened to write list */ public boolean isOpenedToWrite(String filePath, boolean addToList) { int index = openedToWrite.indexOf(filePath); if (index != -1) return true; else { if (addToList) openedToWrite.addElement(filePath); return false; } } //-------- //-------- Page-Bitmap methods //-------- /** * Check to see if this memory cache should handle the page bitmap. * * @return true if this memory cache should handle the page bitmap */ public boolean handlePageBitmap() { return !(pbmBank == null); } /** * Mark the specified page as used in the page bitmap. * * @param page number to mark as used */ public void markPageUsed(int page) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___markPageUsed " + page); // mark page used in cached bitmap of used pages Bit.arrayWriteBit(USED, pbmBitOffset + page, pbmByteOffset, pbmCacheModified); } /** * free the specified page as being un-used in the page bitmap * * @param page number to mark as un-used * * @return true if the page as be been marked as un-used, false * if the page is on an OTP device and cannot be freed */ public boolean freePage(int page) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.print("___freePage " + page); // only free pages that have been written to cache // but not flushed to device if (Bit.arrayReadBit(pbmBitOffset + page, pbmByteOffset, pbmCache) == NOT_USED) { Bit.arrayWriteBit(NOT_USED, pbmBitOffset + page, pbmByteOffset, pbmCacheModified); //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_ was cached so really free now "); return true; } else { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_ not cached so not free"); return false; } } /** * Get the first free page from the page bitmap. * * @return first page number that is free to write */ public int getFirstFreePage() { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.print("___getFirstFreePage "); lastFreePage = 0; return getNextFreePage(); } /** * Get the next free page from the page bitmap. * * @return next page number that is free to write */ public int getNextFreePage() { for (int pg = lastFreePage; pg < totalPages; pg++) { if (Bit.arrayReadBit( pbmBitOffset + pg, pbmByteOffset, pbmCacheModified) == NOT_USED) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___getNextFreePage " + pg); lastFreePage = pg + 1; return pg; } } //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___getNextFreePage, no free pages "); return -1; } /** * Get the total number of free pages in this Filesystem. * * @return number of pages free * * @throws OneWireException when an IO exception occurs */ public int getNumberFreePages() throws OneWireException { // check if need to read the page bitmap for the first time if (!pbmRead) { // read the pbm pbmBank.read(0, false, pbmCache, 0, pbmCache.length); // make a copy of it System.arraycopy(pbmCache, 0, pbmCacheModified, 0, pbmCache.length); // mark as read pbmRead = true; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) { System.out.print("_Page bitmap read in getNumberFreePages: "); debugDump(pbmCache, 0, pbmCache.length); } } int free_pages = 0; for (int pg = 0; pg < totalPages; pg++) { if (Bit.arrayReadBit( pbmBitOffset + pg, pbmByteOffset, pbmCacheModified) == NOT_USED) free_pages++; } //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___getNumberFreePages = " + free_pages); return free_pages; } /** * Gets the page number used in the remote page bitmap in an OTP device. * * @return page number used in the directory for the remote page bitmap */ public int getBitMapPageNumber() { return (pbmBank.getStartPhysicalAddress() / pbmBank.getPageLength()); } /** * Get the number of pages used for the remote page bitmap in an * OTP device. * * @return number of pages used in page bitmap */ public int getBitMapNumberOfPages() { return ((totalPages / 8) / pbmBank.getPageLength()); } /** * Get's the memory bank object for the specified page. * This is significant if the Filesystem spans memory banks * on the same or different devices. */ public PagedMemoryBank getMemoryBankForPage(int page) { int cnt=0; for (int bank_num = 0; bank_num < banks.size(); bank_num++) { // check if 'page' is in this memory bank if ((cnt + bankPages[bank_num]) > page) return (PagedMemoryBank) banks.elementAt(bank_num); cnt += bankPages[bank_num]; } // page provided is not in this Filesystem return null; } /** * Get's the index into the array of Devices where this page * resides. * This is significant if the Filesystem spans memory banks * on the same or different devices. */ private int getDeviceIndex(int page) { for (int dev_num = (startPages.length - 1); dev_num >= 0; dev_num--) { // check if 'page' is in this memory bank if (startPages[dev_num] < page) return dev_num; } // page provided is not in this Filesystem return 0; } /** * Get's the local page number on the memory bank object for * the specified page. * This is significant if the Filesystem spans memory banks * on the same or different devices. */ public int getLocalPage(int page) { int cnt=0; for (int bank_num = 0; bank_num < banks.size(); bank_num++) { // check if 'page' is in this memory bank if ((cnt + bankPages[bank_num]) > page) return (page - cnt); cnt += bankPages[bank_num]; } // page provided is not in this Filesystem return 0; } /** * Clears the lastPageRead global so that a readPage will * not try to continue where the last page left off. * This should be called anytime exclusive access to the * 1-Wire canot be guaranteed. */ public void clearLastPageRead() { // last page can't be used due to redirect read lastPageRead = NONE; } /** * Read the page bitmap. * * @throws OneWireException when an IO exception occurs */ private void readPageBitMap() throws OneWireException { // read the pbm pbmBank.read(0, false, pbmCache, 0, pbmCache.length); // make a copy of it System.arraycopy(pbmCache, 0, pbmCacheModified, 0, pbmCache.length); // mark as read pbmRead = true; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) { System.out.print("____Page bitmap read: "); debugDump(pbmCache, 0, pbmCache.length); } } //-------- //-------- Misc Utility Methods //-------- /** * Debug dump utility method * * @param buf buffer to dump * @param offset offset to start in the buffer * @param len length to dump */ private void debugDump(byte[] buf, int offset, int len) { for (int i = offset; i < (offset + len); i++) { System.out.print(Integer.toHexString((int) buf [i] & 0x00FF) + " "); } System.out.println(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?