void _mmap_page(enum dex_sect_id id, uint idx) { struct dex_section_s *s = &dex_section[id]; u64 offs = 0; uint o_align = 0; if (s->mmap_page) munmap(s->mmap_page, s->cur_size); if (idx > s->nof_entries) dub_die("Dex paging fault. Idx %u > %u.", idx, s->nof_entries); offs = s->toc[idx].offs + s->sect_offs; o_align = offs & (getpagesize() - 1); s->first_item = idx; s->cur_offs = s->toc[idx].offs; s->last_item = find_last(s, idx); /* +4096 tries to make sure that our slack bit-twidling routines * in pcode handling can't cause a segmentation fault */ s->cur_size = s->toc[s->last_item + 1].offs - s->cur_offs + o_align + 4096; s->mmap_page = mmap64(0, s->cur_size, PROT_READ, MAP_SHARED, s->sect_fd, offs - o_align); if (s->mmap_page == MAP_FAILED) dub_sysdie("Couldn't mmap page (offs: %llu size: %llu)", offs - o_align, s->cur_size); s->cur_start = s->mmap_page + o_align; }
bool is_palindrome( Node * head ) { Node * p1 = head; Node * p2 = find_last( p1, nullptr ); while ( p1 != p2 ) { if ( p1->c != p2->c ) return false; else { p1 = p1->next; p2 = find_last( p1, p2 ); if ( ! p2 ) break; } } return true; }
int main() { // Initialize a list struct node *head = malloc(sizeof(struct node)); head->value = 7; for (int i = 7; i > 0; --i) { head = add_to_list(head, i); if (head == NULL) { return EXIT_FAILURE; } } // Delete 1 and 4 delete_from_list(&head, 1); delete_from_list(&head, 4); // Write out the list for (struct node *iter = head; iter != NULL; iter = iter->next) { printf("%d ", iter->value); } printf("\b\n"); // Search 5 struct node *found = search_list(head, 5); if (found != NULL) { printf("%d found\n", found->value); } // Search 10 found = search_list(head, 10); if (found == NULL) { printf("10 not found\n"); } // Count 7 and 2 printf("The list contains %d seven\n", count_occurrencies(head, 7)); printf("The list contains %d two\n", count_occurrencies(head, 2)); // Find last 7 struct node *last = find_last(head, 7); if (last->next != NULL) { fprintf(stderr, "That seven is not the last one\n"); } else { printf("Found %d\n", last->value); } // Find 1 last = find_last(head, 1); if (last != NULL) { fprintf(stderr, "Found an inexistent element?!\n"); } else { printf("1 not found\n"); } // Free memory clear_list(head); return EXIT_SUCCESS; }
/* search the array for "key" * if it is not there, return "-1" */ static int find_item(const t_symbol *key, t_symbol **names, int maxentries) { /* returns index (0..[maxentries-1?]) on success; -1 if the item could not be found */ int i=-1; int max = find_last(names, maxentries); while (++i<=max) if (names[i] && key==names[i]) return i; return -1; }
char *ft_strinside(char *str, char start, char end) { int first; int last; char *inside; last = find_last(str, start); first = find_first(str, last, end); inside = ft_strsub(str, last + 1, first - last - 1); return (inside); }
vector<int> searchRange(vector<int> &A, int target) { // write your code here int length = A.size(); if(length == 0) return vector<int>(2, -1); int start = -1, end = -1; start = find_first(A, target); if(start != -1) end = find_last(A, target); vector<int> result; result.push_back(start); result.push_back(end); return result; }
static int resize_heap() { int e = cur_last_bitmap_entry; int b = find_last(e, 1); unsigned long addr = heap_start + HALLOC_CHUNK_SIZE * (BITS_PER_ENTRY * e + b + 1); if (addr == heap_end) { return 1; } if (kapi_brk(addr)) { heap_end = addr; return 1; } return 0; }
int main (void) { int num = 5; struct node *my_node = malloc(sizeof(struct node)); my_node->value = num; my_node->next = NULL; read_numbers(); printf("my_node add: %p\n", (void *)my_node); print_list(top); top = insert_into_ordered_list(top, my_node); print_list(top); print_list(find_last(top, 2)); delete_from_list(&top, num); print_list(top); return 0; }
int Writer_Task::svc (void) { ACE_Profile_Timer timer; ACE_Profile_Timer::ACE_Elapsed_Time elapsed_time; barrier_.wait (); // Wait at the barrier // We start an ACE_Profile_Timer here... timer.start (); for (size_t iterations = 1; iterations <= n_iterations; iterations++) { ACE_Thread::yield (); #if defined (RW_MUTEX) ACE_Write_Guard<ACE_RW_Thread_Mutex> g (rw_mutex); #else ACE_Guard<ACE_Thread_Mutex> g (mutex); #endif /* RW_MUTEX */ find_last (); current_writers--; } // Stop the timer. timer.stop (); timer.elapsed_time (elapsed_time); this->time_Calculation_.report_time (elapsed_time); return 0; }
/* Make sure we have first/last for this area. */ static int make_firstlast( MergeInfo *inf, Overlapping *ovlap, Rect *oreg ) { REGION *rir = inf->rir; REGION *sir = inf->sir; Rect rr, sr; int y, yr, ys; int missing; /* We're going to build first/last ... lock it from other generate * threads. In fact it's harmless if we do get two writers, but we may * avoid duplicating work. */ g_mutex_lock( ovlap->fl_lock ); /* Do we already have first/last for this area? Bail out if we do. */ missing = 0; for( y = oreg->top; y < IM_RECT_BOTTOM( oreg ); y++ ) { const int j = y - ovlap->overlap.top; const int first = ovlap->first[j]; if( first < 0 ) { missing = 1; break; } } if( !missing ) { /* No work to do! */ g_mutex_unlock( ovlap->fl_lock ); return( 0 ); } /* Entire width of overlap in ref for scan-lines we want. */ rr.left = ovlap->overlap.left; rr.top = oreg->top; rr.width = ovlap->overlap.width; rr.height = oreg->height; rr.left -= ovlap->rarea.left; rr.top -= ovlap->rarea.top; /* Entire width of overlap in sec for scan-lines we want. */ sr.left = ovlap->overlap.left; sr.top = oreg->top; sr.width = ovlap->overlap.width; sr.height = oreg->height; sr.left -= ovlap->sarea.left; sr.top -= ovlap->sarea.top; #ifdef DEBUG printf( "im__lrmerge: making first/last for areas:\n" ); printf( "ref: left = %d, top = %d, width = %d, height = %d\n", rr.left, rr.top, rr.width, rr.height ); printf( "sec: left = %d, top = %d, width = %d, height = %d\n", sr.left, sr.top, sr.width, sr.height ); #endif /* Make pixels. */ if( im_prepare( rir, &rr ) || im_prepare( sir, &sr ) ) { g_mutex_unlock( ovlap->fl_lock ); return( -1 ); } /* Make first/last cache. */ for( y = oreg->top, yr = rr.top, ys = sr.top; y < IM_RECT_BOTTOM( oreg ); y++, yr++, ys++ ) { const int j = y - ovlap->overlap.top; int *first = &ovlap->first[j]; int *last = &ovlap->last[j]; /* Done this line already? */ if( *first < 0 ) { /* Search for start/end of overlap on this scan-line. */ if( find_first( sir, first, sr.left, ys, sr.width ) || find_last( rir, last, rr.left, yr, rr.width ) ) { g_mutex_unlock( ovlap->fl_lock ); return( -1 ); } /* Translate to output space. */ *first += ovlap->sarea.left; *last += ovlap->rarea.left; /* Clip to maximum blend width, if necessary. */ if( ovlap->mwidth >= 0 && *last - *first > ovlap->mwidth ) { int shrinkby = (*last - *first) - ovlap->mwidth; *first += shrinkby / 2; *last -= shrinkby / 2; } } } g_mutex_unlock( ovlap->fl_lock ); return( 0 ); }
int Reader_Task::svc (void) { ACE_Profile_Timer timer; ACE_Profile_Timer::ACE_Elapsed_Time elapsed_time; barrier_.wait (); // Wait at the barrier. // We start an ACE_Profile_Timer here... timer.start (); for (size_t iterations = 1; iterations <= n_iterations; iterations++) { ACE_Thread::yield (); int result = 0; { #if defined (RW_MUTEX) ACE_Read_Guard<ACE_RW_Thread_Mutex> g (rw_mutex); #else ACE_Guard<ACE_Thread_Mutex> g (mutex); #endif /* RW_MUTEX */ find_last (); #if defined (RW_MUTEX) if (use_try_upgrade) result = rw_mutex.tryacquire_write_upgrade (); #endif /* RW_MUTEX */ // True, when we were able to upgrade. if (result == 0 && use_try_upgrade) { //find_last (); try to find something which is not in //there upgraded++; continue; } } if (result == -1 && errno == EBUSY // we tried and failed || !use_try_upgrade) // we did not try at all { #if defined (RW_MUTEX) ACE_Write_Guard<ACE_RW_Thread_Mutex> g (rw_mutex); #else ACE_Guard<ACE_Thread_Mutex> g (mutex); #endif /* RW_MUTEX */ not_upgraded++; find_last (); } else if (result == -1 && errno != EBUSY) ACE_ERROR ((LM_ERROR, ACE_TEXT (" (%t) failure in upgrading to write lock!\n"), 1)); } // Stop the timer. timer.stop (); timer.elapsed_time (elapsed_time); this->time_Calculation_.report_time (elapsed_time); return 0; }
#include <stdlib.h> void print_node(node *print) { printf("Key: %d Value: %d Next: %d Address: %d\n", print->key, print->value, print->next, print); } node * find_last(node *node) { return node->next == 0 ? node : find_last(node->next); } int list_append(node *root, node *new) { node *last = find_last(root); last->next = new; new->key = last->key + 1; return new->key; } node * node_new(int value) { node *new = malloc(sizeof(node)); new->key = 0; new->next = 0; new->value = value; return new; } node * list_search(node *current, int key) {
int main (int argc, char * argv[]) { unsigned char *disk; int fd = open(argv[1], O_RDWR); disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(disk == MAP_FAILED) { perror("mmap"); exit(1); } // Error messages verify the paths and output a corresponding error message struct ext2_inode *final_first_path = find_last(disk, argv[2]); if (final_first_path == NULL) { printf("The file by this name doesn't\n"); return ENOENT; } else if (final_first_path->i_mode & EXT2_FT_DIR) { printf("The file you specified is a directory, not a file\n"); return EISDIR; } struct ext2_inode *final_second_path = find_last(disk, argv[3]); if (final_second_path != NULL && final_second_path->i_mode & EXT2_S_IFDIR) { printf("The second path exists and is a directory\n"); return EISDIR; } else if (final_second_path != NULL) { printf("A file by this name already exists\n"); return EEXIST; } if (find_last(disk, split(argv[3])) == NULL) { printf("The second directory doesn't exist\n"); return ENOENT; } // Find the parent inode of the final entry in the absolute path (of the first provided path) struct ext2_inode *first_split = find_last(disk, split(argv[2])); if (first_split == NULL) { printf("The first directory doesn't exist\n"); return 0; } unsigned int save_inode; int i = 0; // Loop through the inode's blocks for (i = 0; i<12; i++) { if (first_split->i_block[i] > 0) { // Pointer to the directory struct ext2_dir_entry_2 *dir_entries = (struct ext2_dir_entry_2*)(disk + EXT2_BLOCK_SIZE * first_split->i_block[i]); int incremented = 0; // The directory entries are of size 1024 while (incremented < 1024) { char* file_name = &(argv[2][strlen(split(argv[2]))]); // Check if the name of the directory matches the last item in the absolute path (the file) if (strncmp(dir_entries->name, file_name, dir_entries->name_len) == 0) { // Save the inode that belongs to the file save_inode = dir_entries->inode; // End the loops i = 12; break; } // Access the next block through pointer arithmetic and stop the loop once 1024 bytes are read. incremented += dir_entries->rec_len; dir_entries = (struct ext2_dir_entry_2*)((char*) dir_entries + dir_entries->rec_len); } } } // Verify that the absolute path is a directory which will store a link to a file final_second_path = find_last(disk, split(argv[3])); if (final_second_path == NULL) { printf("The second path does not contain a directory\n"); return 0; } // Get the name of the that will be set to the new link char* file_name = &(argv[3][strlen(split(argv[3]))]); // Loop through the blocks while they have been initialized for (i = 0; i<12 && final_second_path->i_block[i] != 0; i++) { // Loop through all the direct blocks the inode (the first 12) if (final_second_path->i_block[i] > 0) { // Pointer to the directory entries struct ext2_dir_entry_2 *dir_entries = (struct ext2_dir_entry_2*)(disk + EXT2_BLOCK_SIZE * final_second_path->i_block[i]); int incremented = 0; // The directory entries are of size 1024 while (incremented != 1024) { // 4 byte alignment of the names int file_size = strlen(file_name); if (file_size % 4 > 0) { file_size += 4 - (file_size % 4); } int dir_name = strlen(dir_entries->name); if (dir_name % 4 > 0) { dir_name += 4 - dir_name % 4; } // If a directory entry is large enough to hold both of the directory entries if (dir_entries->rec_len >= file_size + dir_name + (2 * sizeof(struct ext2_dir_entry_2))) { // Save the space after the required rec_len to asign to the next link in the list int save_space = dir_entries->rec_len - (dir_name + sizeof(struct ext2_dir_entry_2)); // Re-adjust the rec_len to make it appropriately sized dir_entries->rec_len = (dir_name + sizeof(struct ext2_dir_entry_2)); // Make a new dir_entry struct ext2_dir_entry_2 *new_entry = (struct ext2_dir_entry_2 *) ((char*) dir_entries + dir_entries->rec_len); // Set the inode of the link to be the same inode as the original file which we saved new_entry->inode = save_inode; // Make the length and the name correspond to the dir_entry new_entry->rec_len = save_space; new_entry->name_len = strlen(file_name); new_entry->file_type = EXT2_FT_REG_FILE; strncpy(new_entry->name, file_name, strlen(file_name)); i = 12; break; } // Access the next block through pointer arithmetic and stop the loop once 1024 bytes are read. incremented += dir_entries->rec_len; dir_entries = (struct ext2_dir_entry_2*)((char*) dir_entries + dir_entries->rec_len); } } // This is where the implementation of the block allocation should be else if (i == 12 && final_second_path->i_block[i] == 0) { printf("There was no space"); return 0; } } struct ext2_inode *final_inode = find_last(disk, argv[2]); if (final_inode == NULL) { printf("No such file or diretory\n"); return ENOENT; } return 0; }
constexpr const char* get_file_name(conststring s) { return find_last(s, '/'); }
constexpr const char* find_last(conststring s, char ch) { return find_last(s, s.size() - 1, ch); }
constexpr const char* find_last(conststring s, size_t offset, char ch) { return offset == 0 ? s.get_ptr(0) : (s[offset] == ch ? s.get_ptr(offset + 1) : find_last(s, offset - 1, ch)); }
int lt_lookup_db(struct lookup_db *ldb, struct http_info *http_info) { void **vals=NULL; void *ret = NULL; char *s, *snext, *e, *end, store; int len, full_url =0; struct ci_lookup_table *lt_db = (struct ci_lookup_table *)ldb->db_data; switch(ldb->check) { case CHECK_HOST: ret = lt_db->search(lt_db, http_info->site, &vals); break; case CHECK_DOMAIN: s = http_info->site; s--; /* :-) */ do { s++; ci_debug_printf(5, "Checking domain %s ....\n", s); ret = lt_db->search(lt_db, s, &vals); lt_db->release_result(lt_db, vals); } while (!ret && (s=strchr(s, '.'))); break; case CHECK_FULL_URL: full_url = 1; case CHECK_URL: /*for www.site.com/to/path/page.html need to test: www.site.com/to/path/page.html www.site.com/to/path/ www.site.com/to/ www.site.com/ site.com/to/path/page.html site.com/to/path/ site.com/to/ site.com/ com/to/path/page.html com/to/path/ com/to/ com/ */ s = http_info->url; if (!full_url && http_info->args) end = http_info->args; else { len = strlen(http_info->url); end = s+len; } s--; do { s++; e = end; /*Point to the end of string*/ snext = strpbrk(s, "./"); if(!snext || *snext == '/') /*Do not search the top level domains*/ break; do { store = *e; *e = '\0'; /*cut the string exactly here (the http_info->url must not change!) */ ci_debug_printf(9,"Going to check url: %s\n", s); ret = lt_db->search(lt_db, s, &vals); lt_db->release_result(lt_db, vals); *e = store; /*... and restore string to its previous state :-), the http_info->url must not change */ if (full_url && e > http_info->args) e = http_info->args; else e = find_last(s, e-1, '/' ); } while(!ret && e); } while (!ret && (s = snext)); break; case CHECK_SRV_IP: break; case CHECK_SRV_NET: break; default: /*nothing*/ break; } if(vals) lt_db->release_result(lt_db,vals); return (ret != NULL); }
node * find_last(node *node) { return node->next == 0 ? node : find_last(node->next); }