Пример #1
0
    def worst_bitwise(self, min_bytes):
        highest_sum = 0
        highest_count = 0
        highest_idx = None

        b_flat = self.blocks_used.flat
        bm_flat = self.bytes_map.flat

        count = 0
        soma = 0

        for i in self.b:
            if not b_flat[i]:
                count += 1
                soma += bm_flat[i]
            else:
                count = 0
                soma = 0

            if count >= highest_count and min_bytes <= soma >= highest_sum:
                highest_count = count
                highest_sum = soma
                highest_idx = i

        if highest_idx is None:
            raise BadAlloc("Espaço insuficiente")

        print(
            f"[Worst fit] Bytes: {soma} Indice: {highest_idx} Blocos: {highest_count}"
        )
        return highest_idx, highest_count, highest_sum
Пример #2
0
    def c_first(self, obj, force_size=None):
        blocks = force_size
        if not force_size or not isinstance(force_size, int):
            blocks = math.ceil(sys.getsizeof(obj) / block_size)

        count = cpp.first(Heap.blocks_used, blocks,
                          Heap.n_rows * Heap.n_blocks)
        if count != blocks:
            raise BadAlloc("Not enough space")
Пример #3
0
    def first(self, obj, force_size=None):
        blocks = force_size
        if not force_size or not isinstance(force_size, int):
            blocks = math.ceil(sys.getsizeof(obj) / Bucket.size)

        for k in (i for i in self.b if not self.buckets_used.flat[i]):
            ind = np.unravel_index((range(k, k + blocks)), (Heap.n_rows, Heap.n_buckets))
            if (self.buckets_used[ind] == False).all():
                Heap.count += 1
                self.buckets_used[ind] = True
                for i, j in zip(ind[0], ind[1]):
                    self.buckets[i][j].data = obj
                return self.buckets[i][j]

        raise BadAlloc("Not enough space")
Пример #4
0
    def allocate(self, obj, force_size=None):
        # TODO: fix continuous
        blocks = force_size
        if not force_size or not isinstance(force_size, int):
            blocks = math.ceil(sys.getsizeof(obj) / Bucket.size)

        for i in range(Heap.n_rows):
            for j in range(Heap.n_buckets):
                if (self.buckets_used[i][j:j+blocks] == 0).all():
                    Heap.count += 1
                    self.buckets_used[i][j:j+blocks] = 1
                    for bucket in self.buckets[i][j:j+blocks]:
                        bucket.data = obj
                    return self.buckets[i][j]
        raise BadAlloc("Not enough space.")
Пример #5
0
    def best_bitwise(self, min_bytes):
        """Concluido
        Encontra o melhor espaço que acomode o tamanho requisitado
        """
        block = Block()
        lowest_sum = 1e6
        lowest_count = 1e6
        lowest_idx = None

        # Converte a matriz de forma que possa ser lida de forma contígua
        b_flat = self.blocks_used.flat
        bm_flat = self.bytes_map.flat

        for i in self.b:
            idx = i
            count = 0
            soma = 0
            if b_flat[i] == 0:
                while not b_flat[idx]:
                    count += 1
                    soma += bm_flat[idx]
                    if min_bytes <= soma <= lowest_sum and count <= lowest_count:
                        lowest_idx = idx
                        lowest_count = count
                        lowest_sum = soma
                        break

                    idx += 1
                    if idx >= heap.max_size:
                        break

                    if b_flat[idx] == 1:
                        break

        if lowest_idx is None:
            raise BadAlloc("Espaço Insuficiente")

        final_idx = lowest_idx - lowest_count + 1
        print(
            f"[Best fit] Bytes: {lowest_sum} Indice: {final_idx} Blocos: {lowest_count}"
        )
        block.set_data(final_idx, lowest_count, lowest_sum)
        self.blocks_used[
            block.
            indexes] = 1  # Atualiza a matriz para indicar as posições utilizadas

        return block
Пример #6
0
    def first_bitwise(self, min_bytes):
        b_flat = self.blocks_used.flat
        bm_flat = self.bytes_map.flat

        count = 0
        soma = 0

        for i in self.b:
            if not b_flat[i]:
                count += 1
                soma += bm_flat[i]
            else:
                count = 0
                soma = 0

            if soma >= min_bytes:
                print(f"[First fit] Bytes: {soma} Indice: {i} Blocos: {count}")
                return i, count, soma

        raise BadAlloc("Espaço Insuficiente")
Пример #7
0
    def first(self, obj, force_size=None):
        blocks = force_size
        if not force_size or not isinstance(force_size, int):
            blocks = math.ceil(sys.getsizeof(obj) / block_size)

        used = Heap.blocks_used.flat
        for k in (i for i in self.b if not used[i]):
            ind = np.unravel_index((range(k, k + blocks)),
                                   (Heap.n_rows, Heap.n_blocks))
            if (self.blocks_used[ind] == False).all():
                self.blocks_used[ind] = True
                """
                for i in range(blocks - 1, -1, -1):
                    # Converts flatten index to matrix index
                    # x, y = self.flat_to_2D(k + i, self.n_buckets)
                    x, y = np.unravel_index(k + i, (Heap.n_rows, Heap.n_buckets))
                    self.buckets_used[y][x] = 1
                """
                return

        raise BadAlloc("Not enough space")
Пример #8
0
    def first_bitwise(self, min_bytes):
        """Concluido
        Encontra o primeiro espaço em memória cujo o tamanho seja igual ou maior que o desejado
        """

        block = Block()
        lowest_sum = 1e6
        lowest_count = 1e6
        lowest_idx = None

        # Converte a matriz de forma que possa ser lida de forma contígua
        b_flat = self.blocks_used.flat
        bm_flat = self.bytes_map.flat
        count = 0
        soma = 0
        for i in self.b:
            if b_flat[i] == 0:
                count += 1
                soma += bm_flat[i]
            else:
                count = 0
                soma = 0
            if min_bytes <= soma <= lowest_sum and count <= lowest_count:
                lowest_idx = i
                break

        if lowest_idx is None:
            raise BadAlloc("Espaço Insuficiente")
        else:
            final_idx = lowest_idx - count + 1
            print(
                f"[First fit] Bytes: {soma} Indice: {final_idx} Blocos: {count}"
            )
            block.set_data(final_idx, count, soma)
            self.blocks_used[
                block.
                indexes] = 1  # Atualiza a matriz para indicar as posições utilizadas

        return block
Пример #9
0
    def best_bitwise(self, min_bytes):
        lowest_sum = 1e6
        lowest_count = 1e6
        lowest_idx = None

        b_flat = self.blocks_used.flat
        bm_flat = self.bytes_map.flat

        count = 0
        soma = 0

        for i in self.b:
            if not b_flat[i]:
                count += 1
                soma += bm_flat[i]
            else:
                count = 0
                soma = 0

            if count <= lowest_count and lowest_sum >= soma >= min_bytes:
                lowest_sum = soma
                lowest_count = count
                lowest_idx = i

                count = 0
                soma = 0

            if soma >= min_bytes:
                count = 0
                soma = 0

        if lowest_idx is None:
            raise BadAlloc("Espaço Insuficiente")

        print(
            f"[Best fit] Bytes: {lowest_sum} Indice: {lowest_idx} Blocos: {lowest_count}"
        )
        return lowest_idx, lowest_count, lowest_sum