<commit_before>/**
 * Copyright 2014  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "string.h"
#include "stdlib.h"

typedef uint32_t flash_addr_t;
typedef uint32_t page_size_t;
typedef uint32_t page_count_t;

#ifndef min
// allow standalone use where Arduino/Spark min macro may not be available.
template <class T> T min(T a, T b) {
    return a<b?a:b;
}
#endif


inline uint8_t* as_bytes(void* buf) { return (uint8_t*)buf; }
inline const uint8_t* as_bytes(const void* buf) { return (const uint8_t*)buf; }

/**
 * This must be a multiple of 8.
 */
const uint16_t STACK_BUFFER_SIZE = 128u;

/**
 * Function that performs the data transformation when relocating a page in flash.
 */
typedef void (*TransferHandler)(page_size_t pageOffset, void* data, uint8_t* buf, page_size_t bufSize);

/**
 * Provides an interface to a flash device.
 * Reads/Writes can span multiple pages, and be any arbitrary length or offset.
 */
class FlashDevice {


public:
    virtual ~FlashDevice();

    /**
     * @return The size of each page in this flash device.
     */
    virtual page_size_t pageSize() const=0;

    /**
     * @return The number of pages in this flash device.
     */
    virtual page_count_t pageCount() const=0;

    flash_addr_t length() const {
        return pageAddress(pageCount());
    }

    inline bool write(const void* data, flash_addr_t address, page_size_t length) {
        writeErasePage(data, address, length);
    }
    
    inline bool read(void* data, flash_addr_t address, page_size_t length) {
        readPage(data, address, length);
    }

    inline bool writeString(const char* s, flash_addr_t address) {
        write(s, address, strlen(s));
    }

    
    /**
     * Converts a page index [0,N) into the corresponding read/write address.
     * @param page  The page to convert to an address.
     * @return
     */
    flash_addr_t pageAddress(page_count_t page) const {
        return flash_addr_t(page) * pageSize();
    }

    /**
     * Determines if the given address represents the start of a page.<commit_msg>missing return statements.<commit_after>/**
 * Copyright 2014  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "string.h"
#include "stdlib.h"

typedef uint32_t flash_addr_t;
typedef uint32_t page_size_t;
typedef uint32_t page_count_t;

#ifndef min
// allow standalone use where Arduino/Spark min macro may not be available.
template <class T> T min(T a, T b) {
    return a<b?a:b;
}
#endif


inline uint8_t* as_bytes(void* buf) { return (uint8_t*)buf; }
inline const uint8_t* as_bytes(const void* buf) { return (const uint8_t*)buf; }

/**
 * This must be a multiple of 8.
 */
const uint16_t STACK_BUFFER_SIZE = 128u;

/**
 * Function that performs the data transformation when relocating a page in flash.
 */
typedef void (*TransferHandler)(page_size_t pageOffset, void* data, uint8_t* buf, page_size_t bufSize);

/**
 * Provides an interface to a flash device.
 * Reads/Writes can span multiple pages, and be any arbitrary length or offset.
 */
class FlashDevice {


public:
    virtual ~FlashDevice();

    /**
     * @return The size of each page in this flash device.
     */
    virtual page_size_t pageSize() const=0;

    /**
     * @return The number of pages in this flash device.
     */
    virtual page_count_t pageCount() const=0;

    flash_addr_t length() const {
        return pageAddress(pageCount());
    }

    inline bool write(const void* data, flash_addr_t address, page_size_t length) {
        return writeErasePage(data, address, length);
    }
    
    inline bool read(void* data, flash_addr_t address, page_size_t length) {
        return readPage(data, address, length);
    }

    inline bool writeString(const char* s, flash_addr_t address) {
        return write(s, address, strlen(s));
    }

    
    /**
     * Converts a page index [0,N) into the corresponding read/write address.
     * @param page  The page to convert to an address.
     * @return
     */
    flash_addr_t pageAddress(page_count_t page) const {
        return flash_addr_t(page) * pageSize();
    }

    /**
     * Determines if the given address represents the start of a page.
{'fraction_non_alphanumeric': 0.08934517203107659, 'fraction_numerical': 0.00924898261191269, 'mean_word_length': 3.281076801266825, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6241903', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1594, 'n_words': 638}